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 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) 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 ### 参考代码 +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) 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) 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) 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..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 @@ -40,29 +40,80 @@ ![](../Animation/Animation.gif) ### 代码实现 - -``` +#### C++ +```c++ // 对撞指针 // 时间复杂度: O(n) // 空间复杂度: O(1) 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 +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] +``` + diff --git a/Readme.md b/Readme.md index ae55d475..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.algomooc.com +文章同步博客地址:https://blog.algomooc.com/ ## 汇总 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) 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)