diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..9f11b755 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea/ diff --git a/Week_01/id_6/LeetCode_189_6.php b/Week_01/id_6/LeetCode_189_6.php new file mode 100644 index 00000000..d4296c37 --- /dev/null +++ b/Week_01/id_6/LeetCode_189_6.php @@ -0,0 +1,49 @@ +reverse($nums, 0, $len - 1); + $this->reverse($nums, 0, $k - 1); + $this->reverse($nums, $k, $len - 1); + + return; + } + + /** + * revert an array + * + * @param $nums + * @param $start + * @param $end + */ + private function reverse(&$nums, $start, $end) + { + while ($start < $end) { + $tmp = $nums[$start]; + $nums[$start] = $nums[$end]; + $nums[$end] = $tmp; + + $start ++; + $end --; + } + + return; + } +} + +$nums = [1,2,3,4,5,6,7]; +$sol = new Solution(); +$sol->rotate2($nums, 3); +var_export($nums); \ No newline at end of file diff --git a/Week_01/id_6/LeetCode_21_6.php b/Week_01/id_6/LeetCode_21_6.php new file mode 100644 index 00000000..bf87ef39 --- /dev/null +++ b/Week_01/id_6/LeetCode_21_6.php @@ -0,0 +1,54 @@ +val = $val; } + * } + */ +class Solution { + + /** + * @param ListNode $l1 + * @param ListNode $l2 + * @return ListNode + */ + function mergeTwoLists($l1, $l2) + { + if (null == $l1) return $l2; + if (null == $l2) return $l1; + + $list = new ListNode(0); + $cur = $list; + while (null != $l1 && null != $l2) { + if ($l1->val < $l2->val) { + $cur->next = $l1; + $cur = $cur->next; + $l1 = $l1->next; + } else { + $cur->next = $l2; + $cur = $cur->next; + $l2 = $l2->next; + } + } + + if ($l1 == null) { + $cur->next = $l2; + } + + if ($l2 == null) { + $cur->next = $l1; + } + + return $list->next; + } +} + +class ListNode { + public $val = 0; + public $next = null; + function __construct($val) { $this->val = $val; } +} \ No newline at end of file diff --git a/Week_01/id_6/LeetCode_26_6.php b/Week_01/id_6/LeetCode_26_6.php new file mode 100644 index 00000000..d69a5ddd --- /dev/null +++ b/Week_01/id_6/LeetCode_26_6.php @@ -0,0 +1,29 @@ +removeDuplicates($nums); \ No newline at end of file diff --git a/Week_01/id_6/LeetCode_88_6.php b/Week_01/id_6/LeetCode_88_6.php new file mode 100644 index 00000000..815d5ac7 --- /dev/null +++ b/Week_01/id_6/LeetCode_88_6.php @@ -0,0 +1,51 @@ + 0 && $n > 0) { + if ($nums1[$m - 1] > $nums2[$n - 1]) { + $nums1[$len - 1] = $nums1[$m - 1]; + $m --; + } else { + $nums1[$len - 1] = $nums2[$n - 1]; + $n --; + } + $len --; + } + + while ($n > 0) { + $nums1[$len - 1] = $nums2[$n - 1]; + $n --; + $len --; + } + + return; + } +} + +$nums1 = [2,0]; +$m = 1; + +$nums2 = [1]; +$n = 1; +/* +$nums1 = [1,2,3,0,0,0]; +$m = 3; + +$nums2 = [2,5,6]; +$n = 3;*/ + +$sol = new Solution(); +$sol->merge($nums1, $m, $nums2, $n); +var_export($nums1); diff --git a/Week_01/id_6/NOTE.md b/Week_01/id_6/NOTE.md index 107ea7d6..189d22c3 100644 --- a/Week_01/id_6/NOTE.md +++ b/Week_01/id_6/NOTE.md @@ -1 +1,7 @@ # 学习笔记 + +主要练习了数组和链表的习题,每道题都会根据所学思考不同的算法解法。数组和链表树所有数据结构和算法的基础,用数组和链表去实现其他的数据结构,如堆、栈、树等等,手写一遍代码 ,会更加加深自己对于其中数据结构和算法的掌握。 + +在平时的工作之中,自己会刻意地去想有没有更好、更优化的解决方案,这个业务的时间复杂度是多少、空间复杂度是多少,有没有更加优化的解决方案,可以减少内存占用或者服务器资源占用,自己会更多地去思考这方面的问题,这对于自己的平时工作也是一个很的提升。 + +本周联系的题目不算多,但是自己对于每一个题目的思考有很多,不只是每道题目的解法算法,还有不同的思路,自己选择的算法与数据结构,开拓了自己从前没有思考过的思维,并且能够把这种思维应用到实际的业务和场景中,这是自己最大的收获。 \ No newline at end of file diff --git "a/Week_01/id_6/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\346\200\235\347\273\264\345\257\274\345\233\276.pdf" "b/Week_01/id_6/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\346\200\235\347\273\264\345\257\274\345\233\276.pdf" new file mode 100644 index 00000000..47c89231 Binary files /dev/null and "b/Week_01/id_6/\346\225\260\346\215\256\347\273\223\346\236\204\344\270\216\347\256\227\346\263\225\346\200\235\347\273\264\345\257\274\345\233\276.pdf" differ diff --git a/Week_02/id_6/LeetCode_101_6.php b/Week_02/id_6/LeetCode_101_6.php new file mode 100644 index 00000000..ccf80672 --- /dev/null +++ b/Week_02/id_6/LeetCode_101_6.php @@ -0,0 +1,37 @@ +val = $value; } + * } + */ +class Solution { + + /** + * @param TreeNode $root + * @return Boolean + */ + function isSymmetric($root) + { + $this->isSymmetricRec($root, $root); + } + + /** + * @param TreeNode $left_root + * @param TreeNode $right_root + * @return Boolean + */ + function isSymmetricRec($left_root, $right_root) + { + if ($left_root == null && $right_root == null) return true; + if ($left_root == null || $right_root == null) return false; + if ($left_root->val == $right_root->val) { + return $this->isSymmetricRec($left_root->left, $right_root->right) && $this->isSymmetricRec($left_root->right, $right_root->left); + } + return false; + } +} \ No newline at end of file diff --git a/Week_02/id_6/LeetCode_102_6.php b/Week_02/id_6/LeetCode_102_6.php new file mode 100644 index 00000000..2490120d --- /dev/null +++ b/Week_02/id_6/LeetCode_102_6.php @@ -0,0 +1,41 @@ +val = $value; } + * } + */ +class Solution { + + public $levels = []; + + /** + * @param TreeNode $root + * @return Integer[][] + */ + function levelOrder($root) + { + if ($root == null) return []; + // BFS + $this->bfs($root, 0); + return $this->levels; + + } + + function bfs($root, $level) + { + $this->levels[$level][] = $root->val; + + if ($root->left != null) { + $this->bfs($root->left, $level + 1); + } + + if ($root->right != null) { + $this->bfs($root->right, $level + 1); + } + } +} \ No newline at end of file diff --git a/Week_02/id_6/LeetCode_103_6.php b/Week_02/id_6/LeetCode_103_6.php new file mode 100644 index 00000000..51a38792 --- /dev/null +++ b/Week_02/id_6/LeetCode_103_6.php @@ -0,0 +1,44 @@ +val = $value; } + * } + */ +class Solution { + + public $levels = []; + + /** + * @param TreeNode $root + * @return Integer[][] + */ + function zigzagLevelOrder($root) { + if ($root == null) return []; + // BFS + $this->bfs($root, 0); + return $this->levels; + } + + function bfs($root, $level) + { + if ($level % 2 != 0) { + if (!isset($this->levels[$level])) $this->levels[$level] = []; + array_unshift($this->levels[$level], $root->val); + } else { + $this->levels[$level][] = $root->val; + } + + if ($root->left != null) { + $this->bfs($root->left, $level + 1); + } + + if ($root->right != null) { + $this->bfs($root->right, $level + 1); + } + } +} \ No newline at end of file diff --git a/Week_02/id_6/LeetCode_3_6.php b/Week_02/id_6/LeetCode_3_6.php new file mode 100644 index 00000000..75582df8 --- /dev/null +++ b/Week_02/id_6/LeetCode_3_6.php @@ -0,0 +1,30 @@ +lengthOfLongestSubstring($s)); \ No newline at end of file diff --git a/Week_02/id_6/LeetCode_938_6.php b/Week_02/id_6/LeetCode_938_6.php new file mode 100644 index 00000000..876ee4a6 --- /dev/null +++ b/Week_02/id_6/LeetCode_938_6.php @@ -0,0 +1,32 @@ +val = $value; } + * } + */ +class Solution { + + /** + * @param TreeNode $root + * @param Integer $L + * @param Integer $R + * @return Integer + */ + function rangeSumBST($root, $L, $R) + { + if ($root == null) return $this->sum; + if ($root->val > $R) { + return $this->rangeSumBST($root->left, $L, $R); + } + if ($root->val < $L) { + return $this->rangeSumBST($root->right, $L, $R); + } + return $root->val + $this->rangeSumBST($root->left, $L, $R) + $this->rangeSumBST($root->right, $L, $R); + } + +} \ No newline at end of file diff --git a/Week_02/id_6/LeetCode_98_6.php b/Week_02/id_6/LeetCode_98_6.php new file mode 100644 index 00000000..a3e23e60 --- /dev/null +++ b/Week_02/id_6/LeetCode_98_6.php @@ -0,0 +1,28 @@ +val = $value; } + * } + */ +class Solution { + + public $flag = PHP_INT_MIN; + + /** + * @param TreeNode $root + * @return Boolean + */ + function isValidBST($root) { + if ($root == null) return true; + if ($this->isValidBST($root->left)) { + if ($this->flag > $root->val) return false; + $this->flag = $root->val; + return $this->isValidBST($root->right); + } + } +} \ No newline at end of file diff --git a/Week_02/id_6/NOTE.md b/Week_02/id_6/NOTE.md index 107ea7d6..4d85a097 100644 --- a/Week_02/id_6/NOTE.md +++ b/Week_02/id_6/NOTE.md @@ -1 +1,9 @@ # 学习笔记 + +这一周主要是hash表、二叉树、二叉搜索树的练习。难度选择了在中等难度以上的题目进行练习。每道题目都会先自己思考,多多少少都会有一些想法和思路,但是在具体代码实现细节上面还有些欠缺,大部分题目还需要看过别人的讨论才有具体的实现思路。经过自己实现之后,对于同类型的问题做起来会更加有思路些,还是需要更多地练习。 + +Hash表的特点是使用hash函数构建键值对应关系,插入和查找的时间复杂度是O(1),可以应用到字符串搜索、匹配、数据缓存等场景下。LeetCode第3题构建使用hash表,一次循环即可找到最长子字符串,也是一种空间换时间的解决问题思路。 + +更多的对于二叉树和二叉搜索树进行了练习。主要的解题思路是递归,从做题中发现技巧是对于前序、中序、后序遍历以及BFS、DFS的合理运用,可以解决很多问题,至少可以提供一种解题思路。通过做题也对树的上面的遍历方式的具体实现代码有了更熟练的运用。对于二叉搜索树的解题思路,更多运用到二叉搜索树特性:1.左<根<右;2.每一节点都是二叉搜索树;3.中序遍历是递增数列。应用这三点特性,遇到二叉搜索树的问题,还是能提供不少的解题想法的。 + +还需要更多地做题练习来打开自己的解题思维。 \ No newline at end of file