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/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/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\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)