From d2b72e52055a9214d0643a2beccb75082d0930a3 Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Wed, 29 Jul 2020 12:10:13 +0800 Subject: [PATCH 01/12] Update 0001-Two-Sum.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 多版本代码实现 --- 0001-Two-Sum/Article/0001-Two-Sum.md | 88 ++++++++++++++++++++++++++-- 1 file changed, 83 insertions(+), 5 deletions(-) diff --git a/0001-Two-Sum/Article/0001-Two-Sum.md b/0001-Two-Sum/Article/0001-Two-Sum.md index be84999b..95e8591c 100644 --- a/0001-Two-Sum/Article/0001-Two-Sum.md +++ b/0001-Two-Sum/Article/0001-Two-Sum.md @@ -37,7 +37,7 @@ ![](../Animation/Animation.gif) ### 代码实现 - +#### C++ ``` // 1. Two Sum // https://leetcode.com/problems/two-sum/description/ @@ -62,9 +62,87 @@ public: }; ``` - - - - +#### C +```c +// 1. Two Sum +// https://leetcode.com/problems/two-sum/description/ +// 时间复杂度:O(n) +// 空间复杂度:O(n) +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ +int* twoSum(int* nums, int numsSize, int target, int* returnSize){ + int *ans=(int *)malloc(2 * sizeof(int)); + int i,j; + bool flag=false; + for(i=0;i Date: Wed, 29 Jul 2020 12:20:27 +0800 Subject: [PATCH 02/12] =?UTF-8?q?Update=20LeetCode=E7=AC=AC20=E5=8F=B7?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9A=E6=9C=89=E6=95=88=E7=9A=84=E6=8B=AC?= =?UTF-8?q?=E5=8F=B7.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加Java、python版代码实现 --- ...10\347\232\204\346\213\254\345\217\267.md" | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git "a/notes/LeetCode\347\254\25420\345\217\267\351\227\256\351\242\230\357\274\232\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" "b/notes/LeetCode\347\254\25420\345\217\267\351\227\256\351\242\230\357\274\232\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" index c4d0c9c4..712e0347 100644 --- "a/notes/LeetCode\347\254\25420\345\217\267\351\227\256\351\242\230\357\274\232\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" +++ "b/notes/LeetCode\347\254\25420\345\217\267\351\227\256\351\242\230\357\274\232\346\234\211\346\225\210\347\232\204\346\213\254\345\217\267.md" @@ -71,7 +71,8 @@ ### 代码实现 -``` +#### C++ +```c++ class Solution { public: bool isValid(string s) { @@ -109,7 +110,52 @@ public: } }; ``` +#### Java +```java +class Solution { + public boolean isValid(String s) { + //String open="({["; + //String close="]})"; + + Stack stack = new Stack(); + + if(s.length() % 2 != 0) + return false; + + + for (char c : s.toCharArray()) + { + if (c == '(') stack.push(')'); + else if (c == '{') stack.push('}'); + else if (c == '[') stack.push(']'); + else if (stack.isEmpty() || stack.pop() != c) return false; + } + + return stack.isEmpty(); + } + +} +``` +#### Python +```python +class Solution(object): + def isValid(self, s): + open_list = ["[", "{", "("] + close_list = ["]", "}", ")"] + stack = [] + + for i in s: + if i in open_list: + stack.append(i) + elif i in close_list: + pos=close_list.index(i) + if len(stack)>0 and (open_list[pos] == stack[len(stack)-1]): + stack.pop() + else: + return False + if len(stack) == 0: + return True +``` - -![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/gkcza.png) \ No newline at end of file +![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/gkcza.png) From 129f9a8fcdced46e168b9244f49389ede4c76a85 Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Wed, 29 Jul 2020 12:36:43 +0800 Subject: [PATCH 03/12] Update 0167-Two-Sum-II-Input-array-is-sorted.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加Java、Python代码实现 --- .../0167-Two-Sum-II-Input-array-is-sorted.md | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md b/0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md index 2a031236..ee869ead 100644 --- a/0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md +++ b/0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md @@ -40,8 +40,8 @@ ![](../Animation/Animation.gif) ### 代码实现 - -``` +#### C++ +```c++ // 对撞指针 // 时间复杂度: O(n) // 空间复杂度: O(1) @@ -63,6 +63,50 @@ public: ``` +#### Java +```java +class Solution { + public int[] twoSum(int[] numbers, int target) { + int n = numbers.length; + int left = 0; + int right = n-1; + while(left <= right) + { + if(numbers[left] + numbers[right] == target) + { + return new int[]{left + 1, right + 1}; + } + else if (numbers[left] + numbers[right] > target) + { + right--; + } + else + { + left++; + } + } + + return new int[]{-1, -1}; + } +} +``` +#### Python +```python +class Solution(object): + def twoSum(self, numbers, target): + n = len(numbers) + left,right = 0, n-1 + while left <= right: + if numbers[left]+numbers[right] == target: + return [left+1, right+1] + elif numbers[left]+numbers[right] > target: + right -=1 + else: + left +=1 + + return [-1, -1] +``` + From a86c0a02e893d0708c505c5b96ce8304bd639596 Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Wed, 29 Jul 2020 12:41:46 +0800 Subject: [PATCH 04/12] Update 0167-Two-Sum-II-Input-array-is-sorted.md --- .../0167-Two-Sum-II-Input-array-is-sorted.md | 29 ++++++++++++------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md b/0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md index ee869ead..c1174a98 100644 --- a/0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md +++ b/0167-Two-Sum-II-Input-array-is-sorted/Article/0167-Two-Sum-II-Input-array-is-sorted.md @@ -48,20 +48,27 @@ class Solution { public: vector twoSum(vector& numbers, int target) { - int l = 0, r = numbers.size() - 1; - while(l < r){ - if(numbers[l] + numbers[r] == target){ - int res[2] = {l+1, r+1}; - return vector(res, res+2); + int n = numbers.size(); + int left = 0; + int right = n-1; + while(left <= right) + { + if(numbers[left] + numbers[right] == target) + { + return {left + 1, right + 1}; + } + else if (numbers[left] + numbers[right] > target) + { + right--; + } + else + { + left++; } - else if(numbers[l] + numbers[r] < target) - l ++; - else // numbers[l] + numbers[r] > target - r --; } + return {-1, -1}; } - - +}; ``` #### Java ```java From 1cf14b1d54f371929c1597b0fa11152de6156cae Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Wed, 29 Jul 2020 14:13:03 +0800 Subject: [PATCH 05/12] Update 0002-Add-Two-Numbers.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加Java、Python代码实现 --- .../Article/0002-Add-Two-Numbers.md | 66 ++++++++++++++++++- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/0002-Add-Two-Numbers/Article/0002-Add-Two-Numbers.md b/0002-Add-Two-Numbers/Article/0002-Add-Two-Numbers.md index 34744d1a..275e837e 100644 --- a/0002-Add-Two-Numbers/Article/0002-Add-Two-Numbers.md +++ b/0002-Add-Two-Numbers/Article/0002-Add-Two-Numbers.md @@ -32,7 +32,8 @@ ### 代码实现 -``` +#### C++ +```c++ /// 时间复杂度: O(n) /// 空间复杂度: O(n) /** @@ -70,7 +71,68 @@ public: }; ``` - +#### Java +```java +class Solution { + public ListNode addTwoNumbers(ListNode l1, ListNode l2) { + ListNode dummyHead = new ListNode(0); + ListNode cur = dummyHead; + int carry = 0; + + while(l1 != null || l2 != null) + { + int sum = carry; + if(l1 != null) + { + sum += l1.val; + l1 = l1.next; + } + if(l2 != null) + { + sum += l2.val; + l2 = l2.next; + } + // 创建新节点 + carry = sum / 10; + cur.next = new ListNode(sum % 10); + cur = cur.next; + + } + if (carry > 0) { + cur.next = new ListNode(carry); + } + return dummyHead.next; + } +} +``` +#### Python +```python +class Solution(object): + def addTwoNumbers(self, l1, l2): + res=ListNode(0) + head=res + carry=0 + while l1 or l2 or carry!=0: + sum=carry + if l1: + sum+=l1.val + l1=l1.next + if l2: + sum+=l2.val + l2=l2.next + # set value + if sum<=9: + res.val=sum + carry=0 + else: + res.val=sum%10 + carry=sum//10 + # creat new node + if l1 or l2 or carry!=0: + res.next=ListNode(0) + res=res.next + return head +``` ![](../../Pictures/qrcode.jpg) From d9bf519107dc04c868041f32db6db65f4ef514cf Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Wed, 29 Jul 2020 15:49:56 +0800 Subject: [PATCH 06/12] Update 0136-Single-Number.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加C、C++、Java、python代码实现 --- .../Article/0136-Single-Number.md | 58 ++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/0136-Single-Number/Article/0136-Single-Number.md b/0136-Single-Number/Article/0136-Single-Number.md index 3f778c65..8bae68bb 100644 --- a/0136-Single-Number/Article/0136-Single-Number.md +++ b/0136-Single-Number/Article/0136-Single-Number.md @@ -51,6 +51,62 @@ ![](../Animation/136.gif) +### 代码实现 +#### C +````c +int singleNumber(int* nums, int numsSize){ + int res=0; + for(int i=0;i& nums) { + int res=0; + for(auto n:nums) + { + // 异或 + res ^= n; + } + return res; + } +}; +```` + +#### Java +````java +class Solution { + public int singleNumber(int[] nums) { + int res = 0; + for(int n:nums) + { + // 异或 + res ^= n; + } + return res; + } +} +```` + +#### pyton +````python +class Solution(object): + def singleNumber(self, nums): + return reduce(lambda x,y:x^y, nums) +# reduce用法举例 +# 计算列表和:1+2+3+4+5 +# 使用 lambda 匿名函数 +# reduce(lambda x, y: x+y, [1,2,3,4,5]) +```` + ### 进阶版 有一个 n 个元素的数组,除了两个数只出现一次外,其余元素都出现两次,让你找出这两个只出现一次的数分别是几,要求时间复杂度为 O(n) 且再开辟的内存空间固定(与 n 无关)。 @@ -83,4 +139,4 @@ -![](../../Pictures/qrcode.jpg) \ No newline at end of file +![](../../Pictures/qrcode.jpg) From 5c8e809de03d85886606595b46a8e3cbe45d3cc5 Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Fri, 31 Jul 2020 16:19:50 +0800 Subject: [PATCH 07/12] Update 0137-Single-Number-II.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加C++、Java、Python代码实现 --- .../Article/0137-Single-Number-II.md | 42 ++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/0137-Single-Number-II/Article/0137-Single-Number-II.md b/0137-Single-Number-II/Article/0137-Single-Number-II.md index 0c82b30a..245e9abb 100644 --- a/0137-Single-Number-II/Article/0137-Single-Number-II.md +++ b/0137-Single-Number-II/Article/0137-Single-Number-II.md @@ -46,5 +46,45 @@ ![](../Animation/137.gif) +### 代码实现 +#### C++ +```c++ +class Solution { +public: + int singleNumber(vector& nums) { + int one=0, two=0; + for(int n:nums) + { + one = (one ^ n) & (~two); + two = (two ^ n) & (~one); + } + return one; + } +}; +``` +#### Java +```java +class Solution { + public int singleNumber(int[] nums) { + int one=0, two=0; + for(int n:nums) + { + one = (one ^ n) & (~two); + two = (two ^ n) & (~one); + } + return one; + } +} +``` +#### Python +```python +class Solution(object): + def singleNumber(self, nums): + one = two = 0 + for n in nums: + one = (one ^ n) & (~two) + two = (two ^ n) & (~one) + return one +``` -![](../../Pictures/qrcode.jpg) \ No newline at end of file +![](../../Pictures/qrcode.jpg) From 966ccbe6314c401fa9a4fc988bdd90a16cec2975 Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Thu, 13 Aug 2020 19:34:34 +0800 Subject: [PATCH 08/12] =?UTF-8?q?Update=20LeetCode=E7=AC=AC283=E5=8F=B7?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=9A=E7=A7=BB=E5=8A=A8=E9=9B=B6.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加Java、Python语言支持 --- ...32\347\247\273\345\212\250\351\233\266.md" | 42 ++++++++++++++++++- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git "a/notes/LeetCode\347\254\254283\345\217\267\351\227\256\351\242\230\357\274\232\347\247\273\345\212\250\351\233\266.md" "b/notes/LeetCode\347\254\254283\345\217\267\351\227\256\351\242\230\357\274\232\347\247\273\345\212\250\351\233\266.md" index d77ab918..352a9a29 100644 --- "a/notes/LeetCode\347\254\254283\345\217\267\351\227\256\351\242\230\357\274\232\347\247\273\345\212\250\351\233\266.md" +++ "b/notes/LeetCode\347\254\254283\345\217\267\351\227\256\351\242\230\357\274\232\347\247\273\345\212\250\351\233\266.md" @@ -103,8 +103,9 @@ public: ![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/gcetr.gif) 代码如下: +C++ Code: -``` +```c++ // 原地(in place)解决该问题 // 时间复杂度: O(n) // 空间复杂度: O(1) @@ -130,8 +131,45 @@ public: ``` +Java Code: + +```java +class Solution { + public void moveZeroes(int[] nums) { + // 双指针 + int i = 0; + for(int j=0; j None: + """ + Do not return anything, modify nums in-place instead. + """ + # 双指针 + i = 0 + for j in range(len(nums)): + # 不为0,前移 + if nums[j] != 0: + nums[i], nums[j] = nums[j], nums[i] + i+=1 +``` -![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/o6der.png) \ No newline at end of file +![](https://blog-1257126549.cos.ap-guangzhou.myqcloud.com/blog/o6der.png) From 57340038900f88a8d61646447e933c2a22e44a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=BA=8F=E5=91=98=E5=90=B4=E5=B8=88=E5=85=84?= <278166530@qq.com> Date: Mon, 24 Aug 2020 09:27:49 +0800 Subject: [PATCH 09/12] Update Readme.md --- Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Readme.md b/Readme.md index ae55d475..9b2002bb 100755 --- a/Readme.md +++ b/Readme.md @@ -8,13 +8,13 @@ 我会尽力将 LeetCode 上所有的题目都用动画的形式演示出来,计划用 3 到 4 年时间去完成它,期待与你见证这一天! -文章最新首发于微信公众号 **图解面试算法**,您可以关注获取最新的文章。 +文章最新首发于微信公众号 **五分钟学算法**,您可以关注获取最新的文章。 ![](Pictures/qrcode.jpg) -文章同步博客地址:https://www.algomooc.com +文章同步博客地址:https://www.cxyxiaowu.com ## 汇总 From 5cab225759fa309ef844387522fd6a22c6510466 Mon Sep 17 00:00:00 2001 From: Zong <1003160664@qq.com> Date: Wed, 30 Sep 2020 09:43:21 +0800 Subject: [PATCH 10/12] Update 0088-Merge-Sorted-Array.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加C++、Java、Python代码 --- .../Article/0088-Merge-Sorted-Array.md | 90 ++++++++++++++++++- 1 file changed, 89 insertions(+), 1 deletion(-) diff --git a/0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md b/0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md index 7ae9e51d..602e3b50 100644 --- a/0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md +++ b/0088-Merge-Sorted-Array/Article/0088-Merge-Sorted-Array.md @@ -77,6 +77,94 @@ nums2 = [2,5,6], n = 3 Animation ### 参考代码 +C++ Code: + +```c++ +class Solution { +public: + void merge(vector& nums1, int m, vector& nums2, int n) { + int i=m-1, j=n-1, k=m+n-1; + // 合并 + while(i>=0 && j>=0) + { + if(nums1[i] > nums2[j]) + { + nums1[k--] = nums1[i--]; + } + else + { + nums1[k--] = nums2[j--]; + } + } + // 合并剩余的nums2 + while(j>=0) + { + nums1[k--] = nums2[j--]; + } + } +}; +``` + +Java Code: + +```java +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + int i=m-1, j=n-1, k=m+n-1; + // 合并 + while(i>=0 && j>=0) + { + if(nums1[i] > nums2[j]) + { + nums1[k--] = nums1[i--]; + } + else + { + nums1[k--] = nums2[j--]; + } + } + // 合并剩余的nums2 + while(j>=0) + { + nums1[k--] = nums2[j--]; + } + } +} +``` + +Python Code: + +```python +class Solution(object): + def merge(self, nums1, m, nums2, n): + """ + :type nums1: List[int] + :type m: int + :type nums2: List[int] + :type n: int + :rtype: None Do not return anything, modify nums1 in-place instead. + """ + i,j,k = m-1, n-1, m+n-1 + + while i >= 0 and j >= 0: + # print(i,j,k, nums1) + # print(nums1[i], nums2[j]) + if nums1[i] > nums2[j]: + nums1[k] = nums1[i] + k-=1 + i-=1 + else: + nums1[k] = nums2[j] + k-=1 + j-=1 + while j >= 0: + nums1[k] = nums2[j] + k-=1 + j-=1 + +``` + +JavaScript Code: ```javascript /** @@ -105,4 +193,4 @@ var merge = function(nums1, m, nums2, n) { -![](../../Pictures/qrcode.jpg) \ No newline at end of file +![](../../Pictures/qrcode.jpg) From 691a4e5116233b718046e61223d40b0df59d1251 Mon Sep 17 00:00:00 2001 From: jesee030 <“desiasd@qq,comgit config --global user.email “desiasd@qq.com> Date: Tue, 22 Jun 2021 17:55:03 +0800 Subject: [PATCH 11/12] Update 0004-median-of-two-sorted-arrays.md --- .../Article/0004-median-of-two-sorted-arrays.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/0004-median-of-two-sorted-arrays/Article/0004-median-of-two-sorted-arrays.md b/0004-median-of-two-sorted-arrays/Article/0004-median-of-two-sorted-arrays.md index 889ec882..f9ccfa7f 100644 --- a/0004-median-of-two-sorted-arrays/Article/0004-median-of-two-sorted-arrays.md +++ b/0004-median-of-two-sorted-arrays/Article/0004-median-of-two-sorted-arrays.md @@ -42,7 +42,7 @@ nums2 = [3, 4] ![](../Animation/image2.PNG) 如图,我们要找到一组A,B,满足上面3条规则。 对于规则1,我们在数组1中找任意A,然后根据规则1就能推算出对应的B的位置。 -对于规则2,由于数组1和2都是有序数组,即X1 Date: Wed, 12 Jan 2022 15:47:38 +0800 Subject: [PATCH 12/12] Update Readme.md --- Readme.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Readme.md b/Readme.md index 9b2002bb..15cc1ea8 100755 --- a/Readme.md +++ b/Readme.md @@ -8,13 +8,14 @@ 我会尽力将 LeetCode 上所有的题目都用动画的形式演示出来,计划用 3 到 4 年时间去完成它,期待与你见证这一天! -文章最新首发于微信公众号 **五分钟学算法**,您可以关注获取最新的文章。 +文章最新首发于微信公众号 **吴师兄学算法**,您可以关注获取最新的文章。 -![](Pictures/qrcode.jpg) +为了帮助大家更好的入门学习算法,经过半年的积累,我给大家整理了《剑指 Offer》系列的四十道题目,都是算法面试的高频题目,每一道题目我都提供详细的分析、精美的配图、易于理解的动画视频,适合那些第一次刷题的同学,当然,也适合重复刷题的老手再次学习巩固基础。 +![](https://weixin-1257126549.cos.ap-guangzhou.myqcloud.com/blog/qebp5.png) -文章同步博客地址:https://www.cxyxiaowu.com +文章同步博客地址:https://blog.algomooc.com/ ## 汇总