diff --git a/problems/0015.三数之和.md b/problems/0015.三数之和.md index 55e22887..96dc1ac3 100644 --- a/problems/0015.三数之和.md +++ b/problems/0015.三数之和.md @@ -221,6 +221,40 @@ Python: Go: +```Go +func threeSum(nums []int)[][]int{ + sort.Ints(nums) + res:=[][]int{} + + for i:=0;i0{ + break + } + if i>0&&n1==nums[i-1]{ + continue + } + l,r:=i+1,len(nums)-1 + for l=n{ + pre=cur + cur=cur.Next + } + head=head.Next + i++ + } + pre.Next=pre.Next.Next + return result.Next + +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) diff --git a/problems/0020.有效的括号.md b/problems/0020.有效的括号.md index 4dae7072..9f6c8487 100644 --- a/problems/0020.有效的括号.md +++ b/problems/0020.有效的括号.md @@ -181,7 +181,26 @@ class Solution: ``` Go: +```Go +func isValid(s string) bool { + hash := map[byte]byte{')':'(', ']':'[', '}':'{'} + stack := make([]byte, 0) + if s == "" { + return true + } + for i := 0; i < len(s); i++ { + if s[i] == '(' || s[i] == '[' || s[i] == '{' { + stack = append(stack, s[i]) + } else if len(stack) > 0 && stack[len(stack)-1] == hash[s[i]] { + stack = stack[:len(stack)-1] + } else { + return false + } + } + return len(stack) == 0 +} +``` diff --git a/problems/0027.移除元素.md b/problems/0027.移除元素.md index 144cd5be..959474fc 100644 --- a/problems/0027.移除元素.md +++ b/problems/0027.移除元素.md @@ -124,9 +124,19 @@ public: Java: - Python: +```python +class Solution: + def removeElement(self, nums: List[int], val: int) -> int: + i,n = 0,len(nums) + for j in range(n): + if nums[j] != val: + nums[i] = nums[j] + i += 1 + return i +``` + Go: ```go @@ -162,4 +172,4 @@ var removeElement = (nums, val) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
\ No newline at end of file diff --git a/problems/0035.搜索插入位置.md b/problems/0035.搜索插入位置.md index c351e365..e891e3c5 100644 --- a/problems/0035.搜索插入位置.md +++ b/problems/0035.搜索插入位置.md @@ -237,6 +237,22 @@ class Solution { Python: +```python3 +class Solution: + def searchInsert(self, nums: List[int], target: int) -> int: + left, right = 0, len(nums) - 1 + + while left <= right: + middle = (left + right) // 2 + + if nums[middle] < target: + left = middle + 1 + elif nums[middle] > target: + right = middle - 1 + else: + return middle + return right + 1 +``` Go: diff --git a/problems/0053.最大子序和.md b/problems/0053.最大子序和.md index 436e763b..6474d01f 100644 --- a/problems/0053.最大子序和.md +++ b/problems/0053.最大子序和.md @@ -175,8 +175,29 @@ class Solution: ``` Go: +```Go +func maxSubArray(nums []int) int { + if len(nums)<1{ + return 0 + } + dp:=make([]int,len(nums)) + result:=nums[0] + dp[0]=nums[0] + for i:=1;ib{ + return a + }else{ + return b + } +} +``` ----------------------- diff --git a/problems/0070.爬楼梯.md b/problems/0070.爬楼梯.md index 6ae6adc7..f0a08f99 100644 --- a/problems/0070.爬楼梯.md +++ b/problems/0070.爬楼梯.md @@ -230,7 +230,20 @@ class Solution: ``` Go: - +```Go +func climbStairs(n int) int { + if n==1{ + return 1 + } + dp:=make([]int,n+1) + dp[1]=1 + dp[2]=2 + for i:=3;i<=n;i++{ + dp[i]=dp[i-1]+dp[i-2] + } + return dp[n] +} +``` diff --git a/problems/0078.子集.md b/problems/0078.子集.md index a61d7493..7166f7fa 100644 --- a/problems/0078.子集.md +++ b/problems/0078.子集.md @@ -209,6 +209,28 @@ Python: Go: +```Go +var res [][]int +func subset(nums []int) [][]int { + res = make([][]int, 0) + sort.Ints(nums) + Dfs([]int{}, nums, 0) + return res +} +func Dfs(temp, nums []int, start int){ + tmp := make([]int, len(temp)) + copy(tmp, temp) + res = append(res, tmp) + for i := start; i < len(nums); i++{ + //if i>start&&nums[i]==nums[i-1]{ + // continue + //} + temp = append(temp, nums[i]) + Dfs(temp, nums, i+1) + temp = temp[:len(temp)-1] + } +} +``` Javascript: diff --git a/problems/0090.子集II.md b/problems/0090.子集II.md index 47db79f7..61036839 100644 --- a/problems/0090.子集II.md +++ b/problems/0090.子集II.md @@ -211,7 +211,30 @@ Python: Go: +```Go +var res[][]int +func subsetsWithDup(nums []int)[][]int { + res=make([][]int,0) + sort.Ints(nums) + dfs([]int{},nums,0) + return res +} +func dfs(temp, num []int, start int) { + tmp:=make([]int,len(temp)) + copy(tmp,temp) + + res=append(res,tmp) + for i:=start;istart&&num[i]==num[i-1]{ + continue + } + temp=append(temp,num[i]) + dfs(temp,num,i+1) + temp=temp[:len(temp)-1] + } +} +``` diff --git a/problems/0098.验证二叉搜索树.md b/problems/0098.验证二叉搜索树.md index 524d6a07..45f2308b 100644 --- a/problems/0098.验证二叉搜索树.md +++ b/problems/0098.验证二叉搜索树.md @@ -273,7 +273,25 @@ Python: Go: +```Go +import "math" +func isValidBST(root *TreeNode) bool { + if root == nil { + return true + } + return isBST(root, math.MinInt64, math.MaxFloat64) +} +func isBST(root *TreeNode, min, max int) bool { + if root == nil { + return true + } + if min >= root.Val || max <= root.Val { + return false + } + return isBST(root.Left, min, root.Val) && isBST(root.Right, root.Val, max) +} +``` diff --git a/problems/0101.对称二叉树.md b/problems/0101.对称二叉树.md index 561d0470..535300af 100644 --- a/problems/0101.对称二叉树.md +++ b/problems/0101.对称二叉树.md @@ -253,7 +253,6 @@ public: ## 其他语言版本 - Java: @@ -284,4 +283,4 @@ const check = (leftPtr, rightPtr) => { * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
+
\ No newline at end of file diff --git a/problems/0110.平衡二叉树.md b/problems/0110.平衡二叉树.md index b8a373fd..555d7a24 100644 --- a/problems/0110.平衡二叉树.md +++ b/problems/0110.平衡二叉树.md @@ -353,7 +353,6 @@ public: ## 其他语言版本 - Java: ```java class Solution { @@ -384,7 +383,40 @@ Python: Go: - +```Go +func isBalanced(root *TreeNode) bool { + if root==nil{ + return true + } + if !isBalanced(root.Left) || !isBalanced(root.Right){ + return false + } + LeftH:=maxdepth(root.Left)+1 + RightH:=maxdepth(root.Right)+1 + if abs(LeftH-RightH)>1{ + return false + } + return true +} +func maxdepth(root *TreeNode)int{ + if root==nil{ + return 0 + } + return max(maxdepth(root.Left),maxdepth(root.Right))+1 +} +func max(a,b int)int{ + if a>b{ + return a + } + return b +} +func abs(a int)int{ + if a<0{ + return -a + } + return a +} +``` diff --git a/problems/0198.打家劫舍.md b/problems/0198.打家劫舍.md index b9ccec45..c64648ad 100644 --- a/problems/0198.打家劫舍.md +++ b/problems/0198.打家劫舍.md @@ -117,6 +117,33 @@ Python: Go: +```Go +func rob(nums []int) int { + if len(nums)<1{ + return 0 + } + if len(nums)==1{ + return nums[0] + } + if len(nums)==2{ + return max(nums[0],nums[1]) + } + dp :=make([]int,len(nums)) + dp[0]=nums[0] + dp[1]=max(nums[0],nums[1]) + for i:=2;ib{ + return a + } + return b +} +``` diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index d6b1cdd2..8c0dd1e7 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -84,7 +84,28 @@ public: Java: +```java +class Solution { + public boolean isHappy(int n) { + Set record = new HashSet<>(); + while (n != 1 && !record.contains(n)) { + record.add(n); + n = getNextNumber(n); + } + return n == 1; + } + private int getNextNumber(int n) { + int res = 0; + while (n > 0) { + int temp = n % 10; + res += temp * temp; + n = n / 10; + } + return res; + } +} +``` Python: diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 2fa6dc06..7876eb99 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -225,9 +225,22 @@ class Solution { Python: - Go: +```Go +func invertTree(root *TreeNode) *TreeNode { + if root ==nil{ + return nil + } + temp:=root.Left + root.Left=root.Right + root.Right=temp + + invertTree(root.Left) + invertTree(root.Right) + return root +} +``` diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 49798cce..1927f476 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -88,19 +88,19 @@ Java: ```java class Solution { public boolean isAnagram(String s, String t) { - int[] strMap = new int[123]; - for (int i = 0; i < s.length(); i++) { - strMap[s.charAt(i) + 0]++; - } - for (int i = 0; i < t.length(); i++) { - strMap[t.charAt(i) + 0]--; + int[] record = new int[26]; + for (char c : s.toCharArray()) { + record[c - 'a'] += 1; } - - for (int i = 90; i < 123; i++) { - if (strMap[i] != 0) return false; + for (char c : t.toCharArray()) { + record[c - 'a'] -= 1; + } + for (int i : record) { + if (i != 0) { + return false; + } } - return true; } } diff --git a/problems/0344.反转字符串.md b/problems/0344.反转字符串.md index 02282355..b4c843b7 100644 --- a/problems/0344.反转字符串.md +++ b/problems/0344.反转字符串.md @@ -160,6 +160,17 @@ Python: Go: +```Go +func reverseString(s []byte) { + left:=0 + right:=len(s)-1 + for left stack = new Stack<> (); + stack.add(root); + int result = 0; + while (!stack.isEmpty()) { + TreeNode node = stack.pop(); + if (node.left != null && node.left.left == null && node.left.right == null) { + result += node.left.val; + } + if (node.right != null) stack.add(node.right); + if (node.left != null) stack.add(node.left); + } + return result; + } +} +``` + + + Python: @@ -188,4 +216,6 @@ Go: * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) * 知识星球:[代码随想录](https://mp.weixin.qq.com/s/QVF6upVMSbgvZy8lHZS3CQ) -
\ No newline at end of file + +
+ diff --git a/problems/0450.删除二叉搜索树中的节点.md b/problems/0450.删除二叉搜索树中的节点.md index 2670435e..604fb376 100644 --- a/problems/0450.删除二叉搜索树中的节点.md +++ b/problems/0450.删除二叉搜索树中的节点.md @@ -284,6 +284,44 @@ Python: Go: +```Go +func deleteNode(root *TreeNode, key int) *TreeNode { + if root==nil{ + return nil + } + if keyroot.Val{ + root.Right=deleteNode(root.Right,key) + return root + } + if root.Right==nil{ + return root.Left + } + if root.Left==nil{ + return root.Right + } + minnode:=root.Right + for minnode.Left!=nil{ + minnode=minnode.Left + } + root.Val=minnode.Val + root.Right=deleteNode1(root.Right) + return root +} + +func deleteNode1(root *TreeNode)*TreeNode{ + if root.Left==nil{ + pRight:=root.Right + root.Right=nil + return pRight + } + root.Left=deleteNode1(root.Left) + return root +} +``` diff --git a/problems/回溯算法理论基础.md b/problems/回溯算法理论基础.md index 3aba34db..8bfd101c 100644 --- a/problems/回溯算法理论基础.md +++ b/problems/回溯算法理论基础.md @@ -163,10 +163,6 @@ void backtracking(参数) { -![](https://img-blog.csdnimg.cn/20210416110157800.png) - - - ## 其他语言版本 diff --git a/problems/数组总结篇.md b/problems/数组总结篇.md index 7c4c0fed..d8daa866 100644 --- a/problems/数组总结篇.md +++ b/problems/数组总结篇.md @@ -131,9 +131,6 @@ 最后,大家周末愉快! - - - ## 其他语言版本 diff --git a/problems/数组理论基础.md b/problems/数组理论基础.md index 457cd33e..f061088f 100644 --- a/problems/数组理论基础.md +++ b/problems/数组理论基础.md @@ -7,6 +7,7 @@

欢迎大家参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+ ## 数组理论基础 数组是非常基础的数据结构,在面试中,考察数组的题目一般在思维上都不难,主要是考察对代码的掌控能力 @@ -23,6 +24,8 @@ ![算法通关数组](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%84.png) + + 需要两点注意的是 * **数组下标都是从0开始的。** @@ -34,6 +37,7 @@ ![算法通关数组1](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%841.png) + 而且大家如果使用C++的话,要注意vector 和 array的区别,vector的底层实现是array,严格来讲vector是容器,不是数组。 **数组的元素是不能删的,只能覆盖。** @@ -42,110 +46,80 @@ ![算法通关数组2](https://code-thinking.cdn.bcebos.com/pics/%E7%AE%97%E6%B3%95%E9%80%9A%E5%85%B3%E6%95%B0%E7%BB%842.png) + **那么二维数组在内存的空间地址是连续的么?** -不同编程语言的内存管理是不一样的,以C++为例,在C++中二维数组是连续分布的,如图: +不同编程语言的内存管理是不一样的,以C++为例,在C++中二维数组是连续分布的。 + +我们来做一个实验,C++测试代码如下: + +```C++ +void test_arr() { + int array[2][3] = { + {0, 1, 2}, + {3, 4, 5} + }; + cout << &array[0][0] << " " << &array[0][1] << " " << &array[0][2] << endl; + cout << &array[1][0] << " " << &array[1][1] << " " << &array[1][2] << endl; +} + +int main() { + test_arr(); +} + +``` + +测试地址为 + +``` +0x7ffee4065820 0x7ffee4065824 0x7ffee4065828 +0x7ffee406582c 0x7ffee4065830 0x7ffee4065834 +``` + +注意地址为16进制,可以看出二维数组地址是连续一条线的。 + +一些录友可能看不懂内存地址,我就简单介绍一下, 0x7ffee4065820 与 0x7ffee4065824 差了一个4,就是4个字节,因为这是一个int型的数组,所以两个相信数组元素地址差4个字节。 + +0x7ffee4065828 与 0x7ffee406582c 也是差了4个字节,在16进制里8 + 4 = c,c就是12。 + +如图: ![数组内存](https://img-blog.csdnimg.cn/20210310150641186.png) -Java的二维数组可能是如下排列的方式: +**所以可以看出在C++中二维数组在地址空间上是连续的**。 + +像Java是没有指针的,同时也不对程序员暴漏其元素的地址,寻址操作完全交给虚拟机。 + +所以看不到每个元素的地址情况,这里我以Java为例,也做一个实验。 + +```Java +public static void test_arr() { + int[][] arr = {{1, 2, 3}, {3, 4, 5}, {6, 7, 8}, {9,9,9}}; + System.out.println(arr[0]); + System.out.println(arr[1]); + System.out.println(arr[2]); + System.out.println(arr[3]); +} +``` +输出的地址为: + +``` +[I@7852e922 +[I@4e25154f +[I@70dea4e +[I@5c647e05 +``` + +这里的数值也是16进制,这不是真正的地址,而是经过处理过后的数值了,我们也可以看出,二维数组的每一行头结点的地址是没有规则的,更谈不上连续。 + +所以Java的二维数组可能是如下排列的方式: ![算法通关数组3](https://img-blog.csdnimg.cn/20201214111631844.png) -我们在[数组过于简单,但你该了解这些!](https://mp.weixin.qq.com/s/c2KABb-Qgg66HrGf8z-8Og)分别作了实验 +这里面试中数组相关的理论知识就介绍完了。 -## 数组的经典题目 +后续我将介绍面试中数组相关的五道经典面试题目,敬请期待! -在面试中,数组是必考的基础数据结构。 - -其实数据的题目在思想上一般比较简单的,但是如果想高效,并不容易。 - -我们之前一共讲解了四道经典数组题目,每一道题目都代表一个类型,一种思想。 - -### 二分法 - -[704.二分查找](https://mp.weixin.qq.com/s/4X-8VRgnYRGd5LYGZ33m4w) - -在这道题目中我们讲到了**循环不变量原则**,只有在循环中坚持对区间的定义,才能清楚的把握循环中的各种细节。 - -**二分法是算法面试中的常考题,建议通过这道题目,锻炼自己手撕二分的能力**。 - -相关题目: - -* 35.搜索插入位置 -* 34.在排序数组中查找元素的第一个和最后一个位置 -* 69.x 的平方根 -* 367.有效的完全平方数 - -### 双指针法 - -[27. 移除元素](https://mp.weixin.qq.com/s/RMkulE4NIb6XsSX83ra-Ww) - -双指针法(快慢指针法):**通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。** - -暴力解法时间复杂度:O(n^2) -双指针时间复杂度:O(n) - -这道题目迷惑了不少同学,纠结于数组中的元素为什么不能删除,主要是因为一下两点: - -* 数组在内存中是连续的地址空间,不能释放单一元素,如果要释放,就是全释放(程序运行结束,回收内存栈空间)。 -* C++中vector和array的区别一定要弄清楚,vector的底层实现是array,所以vector展现出友好的一些都是因为经过包装了。 - -双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组和链表操作的面试题,都使用双指针法。 - -相关题目: - -* 26.删除排序数组中的重复项 -* 283.移动零 -* 844.比较含退格的字符串 -* 977.有序数组的平方 - -### 滑动窗口 - -[209.长度最小的子数组](https://mp.weixin.qq.com/s/ewCRwVw0h0v4uJacYO7htQ) - -本题介绍了数组操作中的另一个重要思想:滑动窗口。 - -暴力解法时间复杂度:O(n^2) -滑动窗口时间复杂度:O(n) - -本题中,主要要理解滑动窗口如何移动 窗口起始位置,达到动态更新窗口大小的,从而得出长度最小的符合条件的长度。 - -**滑动窗口的精妙之处在于根据当前子序列和大小的情况,不断调节子序列的起始位置。从而将O(n^2)的暴力解法降为O(n)。** - -如果没有接触过这一类的方法,很难想到类似的解题思路,滑动窗口方法还是很巧妙的。 - -相关题目: - -* 904.水果成篮 -* 76.最小覆盖子串 - -### 模拟行为 - -[59.螺旋矩阵II](https://mp.weixin.qq.com/s/Hn6-mlCPvKAdWbiFfQyaaw) - -模拟类的题目在数组中很常见,不涉及到什么算法,就是单纯的模拟,十分考察大家对代码的掌控能力。 - -在这道题目中,我们再一次介绍到了**循环不变量原则**,其实这也是写程序中的重要原则。 - -相信大家又遇到过这种情况: 感觉题目的边界调节超多,一波接着一波的判断,找边界,踩了东墙补西墙,好不容易运行通过了,代码写的十分冗余,毫无章法,其实**真正解决题目的代码都是简洁的,或者有原则性的**,大家可以在这道题目中体会到这一点。 - -相关题目: - -* 54.螺旋矩阵 -* 剑指Offer 29.顺时针打印矩阵 - -## 总结 - -从二分法到双指针,从滑动窗口到螺旋矩阵,相信如果大家真的认真做了「代码随想录」每日推荐的题目,定会有所收获。 - -**每道题目后面都有相关练习题,也别忘了去做做!** - -数组专题中讲解和相关题目已经有16道了,就不介绍太过题目了,因为数组是非常基础的数据结构后面很多专题还会用到数组,所以后面的题目依然会会间接练习数组的。 - - - -![](https://img-blog.csdnimg.cn/20210416110157800.png) ## 其他语言版本