From 8ef63207ac0d7446aac4e8a4261f5856ecb4a252 Mon Sep 17 00:00:00 2001 From: 3119005212 <1136153224@qq.com> Date: Tue, 27 Apr 2021 15:11:05 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=89=8D=E7=BC=80=E5=92=8C=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../前缀和/leetcode1248寻找优美子数组.md | 59 ++++++++++++++++++- .../前缀和/leetcode523连续的子数组和.md | 28 +++++++++ .../前缀和/leetcode560和为K的子数组.md | 33 +++++++++++ .../前缀和/leetcode724寻找数组的中心索引.md | 27 ++++++++- .../前缀和/leetcode974和可被K整除的子数组.md | 25 ++++++++ 5 files changed, 170 insertions(+), 2 deletions(-) diff --git a/animation-simulation/前缀和/leetcode1248寻找优美子数组.md b/animation-simulation/前缀和/leetcode1248寻找优美子数组.md index 1a394c2..55e3405 100644 --- a/animation-simulation/前缀和/leetcode1248寻找优美子数组.md +++ b/animation-simulation/前缀和/leetcode1248寻找优美子数组.md @@ -43,6 +43,8 @@ 我们来解析一下哈希表,key 代表的是含有 1 个奇数的前缀区间,value 代表这种子区间的个数,含有两个,也就是nums[0],nums[0,1].后面含义相同,那我们下面直接看代码吧,一下就能读懂。 +Java Code: + ```java class Solution { public int numberOfSubarrays(int[] nums, int k) { @@ -70,8 +72,40 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int numberOfSubarrays(vector& nums, int k) { + if (nums.size() == 0) { + return 0; + } + map m; + //统计奇数个数,相当于我们的 presum + int oddnum = 0; + int count = 0; + m.insert({0,1}); + for (int & x : nums) { + // 统计奇数个数 + oddnum += x & 1; + // 发现存在,则 count增加 + if (m.find(oddnum - k) != m.end()) { + count += m[oddnum - k]; + } + //存入 + if(m.find(oddnum) != m.end()) m[oddnum]++; + else m[oddnum] = 1; + } + return count; + } +}; +``` + 但是也有一点不同,就是我们是统计奇数的个数,数组中的奇数个数肯定不会超过原数组的长度,所以这个题目中我们可以用数组来模拟 HashMap ,用数组的索引来模拟 HashMap 的 key,用值来模拟哈希表的 value。下面我们直接看代码吧。 +Java Code: + ```java class Solution { public int numberOfSubarrays(int[] nums, int k) { @@ -93,4 +127,27 @@ class Solution { } ``` -### \ No newline at end of file +C++ Code: + +```cpp +class Solution { +public: + int numberOfSubarrays(vector& nums, int k) { + int len = nums.size(); + vector map(len + 1, 0); + map[0] = 1; + int oddnum = 0; + int count = 0; + for (int i = 0; i < len; ++i) { + //如果是奇数则加一,偶数加0,相当于没加 + oddnum += nums[i] & 1; + if (oddnum - k >= 0) { + count += map[oddnum-k]; + } + map[oddnum]++; + } + return count; + } +}; +``` + diff --git a/animation-simulation/前缀和/leetcode523连续的子数组和.md b/animation-simulation/前缀和/leetcode523连续的子数组和.md index 07e9c4e..c84d13b 100644 --- a/animation-simulation/前缀和/leetcode523连续的子数组和.md +++ b/animation-simulation/前缀和/leetcode523连续的子数组和.md @@ -46,6 +46,8 @@ **题目代码** +Java Code: + ```java class Solution { public boolean checkSubarraySum(int[] nums, int k) { @@ -71,5 +73,31 @@ class Solution { } ``` +C++ Code: +```cpp +class Solution { +public: + bool checkSubarraySum(vector& nums, int k) { + map m; + //细节2 + m.insert({0,-1}); + int presum = 0; + for (int i = 0; i < nums.size(); ++i) { + presum += nums[i]; + //细节1,防止 k 为 0 的情况 + int key = k == 0 ? presum : presum % k; + if (m.find(key) != m.end()) { + if (i - m[key] >= 2) { + return true; + } + //因为我们需要保存最小索引,当已经存在时则不用再次存入,不然会更新索引值 + continue; + } + m.insert({key, i}); + } + return false; + } +}; +``` diff --git a/animation-simulation/前缀和/leetcode560和为K的子数组.md b/animation-simulation/前缀和/leetcode560和为K的子数组.md index 972651f..0b5cf13 100644 --- a/animation-simulation/前缀和/leetcode560和为K的子数组.md +++ b/animation-simulation/前缀和/leetcode560和为K的子数组.md @@ -122,6 +122,8 @@ class Solution { **题目代码** +Java Code: + ```java class Solution { public int subarraySum(int[] nums, int k) { @@ -150,3 +152,34 @@ class Solution { } ``` +C++ Code: + +```cpp +public: + int subarraySum(vector& nums, int k) { + if (nums.size() == 0) { + return 0; + } + map m; + //细节,这里需要预存前缀和为 0 的情况,会漏掉前几位就满足的情况 + //例如输入[1,1,0],k = 2 如果没有这行代码,则会返回0,漏掉了1+1=2,和1+1+0=2的情况 + //输入:[3,1,1,0] k = 2时则不会漏掉 + //因为presum[3] - presum[0]表示前面 3 位的和,所以需要map.put(0,1),垫下底 + m.insert({0, 1}); + int count = 0; + int presum = 0; + for (int x : nums) { + presum += x; + //当前前缀和已知,判断是否含有 presum - k的前缀和,那么我们就知道某一区间的和为 k 了。 + if (m.find(presum - k) != m.end()) { + count += m[presum - k];//获取presum-k前缀和出现次数 + } + //更新 + if(m.find(presum) != m.end()) m[presum]++; + else m[presum] = 1; + } + return count; + } +}; +``` + diff --git a/animation-simulation/前缀和/leetcode724寻找数组的中心索引.md b/animation-simulation/前缀和/leetcode724寻找数组的中心索引.md index 70754f5..37ce953 100644 --- a/animation-simulation/前缀和/leetcode724寻找数组的中心索引.md +++ b/animation-simulation/前缀和/leetcode724寻找数组的中心索引.md @@ -61,6 +61,8 @@ 理解了我们前缀和的概念(不知道好像也可以做,这个题太简单了哈哈)。我们可以一下就能把这个题目做出来,先遍历一遍求出数组的和,然后第二次遍历时,直接进行对比左半部分和右半部分是否相同,如果相同则返回 true,不同则继续遍历。 +Java Code: + ```java class Solution { public int pivotIndex(int[] nums) { @@ -82,4 +84,27 @@ class Solution { } ``` -### \ No newline at end of file +C++ Code: + +```cpp +class Solution { +public: + int pivotIndex(vector& nums) { + int presum = 0; + //数组的和 + for (int x : nums) { + presum += x; + } + int leftsum = 0; + for (int i = 0; i < nums.size(); ++i) { + //发现相同情况 + if (leftsum == presum - nums[i] - leftsum) { + return i; + } + leftsum += nums[i]; + } + return -1; + } +}; +``` + diff --git a/animation-simulation/前缀和/leetcode974和可被K整除的子数组.md b/animation-simulation/前缀和/leetcode974和可被K整除的子数组.md index 86656d5..a6991ba 100644 --- a/animation-simulation/前缀和/leetcode974和可被K整除的子数组.md +++ b/animation-simulation/前缀和/leetcode974和可被K整除的子数组.md @@ -87,6 +87,8 @@ int key = (presum % K + K) % K; 那么这个题目我们可不可以用数组,代替 map 呢?当然也是可以的,因为此时我们的哈希表存的是余数,余数最大也只不过是 K-1所以我们可以用固定长度 K 的数组来模拟哈希表。 +Java Code: + ```java class Solution { public int subarraysDivByK(int[] A, int K) { @@ -107,3 +109,26 @@ class Solution { } ``` +C++ Code: + +```cpp +class Solution { +public: + int subarraysDivByK(vector& A, int K) { + vector map (K, 0); + int len = A.size(); + int count = 0; + int presum = 0; + map[0] = 1; + for (int i = 0; i < len; ++i) { + presum += A[i]; + //求key + int key = (presum % K + K) % K; + //count添加次数,并将当前的map[key]++; + count += (map[key]++); + } + return count; + } +}; +``` + From 376bec88eff12bc87dffd23baabd5793788ac588 Mon Sep 17 00:00:00 2001 From: MoriSummer <54202741+coderhare@users.noreply.github.com> Date: Tue, 27 Apr 2021 15:13:00 +0800 Subject: [PATCH 2/6] =?UTF-8?q?Update=20leetcode560=E5=92=8C=E4=B8=BAK?= =?UTF-8?q?=E7=9A=84=E5=AD=90=E6=95=B0=E7=BB=84.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- animation-simulation/前缀和/leetcode560和为K的子数组.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/animation-simulation/前缀和/leetcode560和为K的子数组.md b/animation-simulation/前缀和/leetcode560和为K的子数组.md index 0b5cf13..cf52022 100644 --- a/animation-simulation/前缀和/leetcode560和为K的子数组.md +++ b/animation-simulation/前缀和/leetcode560和为K的子数组.md @@ -164,7 +164,7 @@ public: //细节,这里需要预存前缀和为 0 的情况,会漏掉前几位就满足的情况 //例如输入[1,1,0],k = 2 如果没有这行代码,则会返回0,漏掉了1+1=2,和1+1+0=2的情况 //输入:[3,1,1,0] k = 2时则不会漏掉 - //因为presum[3] - presum[0]表示前面 3 位的和,所以需要map.put(0,1),垫下底 + //因为presum[3] - presum[0]表示前面 3 位的和,所以需要m.insert({0,1}),垫下底 m.insert({0, 1}); int count = 0; int presum = 0; From 39683f1794d576ca3e5531fcbf06546fe05e5ce9 Mon Sep 17 00:00:00 2001 From: daluozha <561890199@qq.com> Date: Tue, 27 Apr 2021 18:12:18 +0800 Subject: [PATCH 3/6] =?UTF-8?q?leetcode=20206=20=E8=A1=A5=E5=85=85js?= =?UTF-8?q?=E7=89=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../链表篇/leetcode206反转链表.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/animation-simulation/链表篇/leetcode206反转链表.md b/animation-simulation/链表篇/leetcode206反转链表.md index 3130b04..93b6261 100644 --- a/animation-simulation/链表篇/leetcode206反转链表.md +++ b/animation-simulation/链表篇/leetcode206反转链表.md @@ -33,6 +33,7 @@ low = temp 即可。然后重复执行上诉操作直至最后,这样则完成 我会对每个关键点进行注释,大家可以参考动图理解。 +Java Code: ```java class Solution { public ListNode reverseList(ListNode head) { @@ -62,9 +63,27 @@ class Solution { } ``` +JS Code: +```javascript +var reverseList = function(head) { + if(!head || !head.next) { + return head; + } + let low = null; + let pro = head; + while (pro) { + let temp = pro; + pro = pro.next; + temp.next = low; + low = temp; + } + return low; +}; +``` 上面的迭代写法是不是搞懂啦,现在还有一种递归写法,不是特别容易理解,刚开始刷题的同学,可以只看迭代解法。 +Java Code: ```java class Solution { public ListNode reverseList(ListNode head) { @@ -86,3 +105,16 @@ class Solution { ``` +JS Code: +```javascript +var reverseList = function(head) { + if (!head || !head.next) { + return head; + } + let pro = reverseList(head.next); + head.next.next = head; + head.next = null; + return pro; +}; +``` + From 4e53da9e3ef1d044fa801465457fe72a818eb633 Mon Sep 17 00:00:00 2001 From: daluozha <561890199@qq.com> Date: Tue, 27 Apr 2021 18:13:08 +0800 Subject: [PATCH 4/6] =?UTF-8?q?leetcode=20225=20=E8=A1=A5=E5=85=85js?= =?UTF-8?q?=E7=89=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../栈和队列/225.用队列实现栈.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/animation-simulation/栈和队列/225.用队列实现栈.md b/animation-simulation/栈和队列/225.用队列实现栈.md index c6b17c8..ccaa942 100644 --- a/animation-simulation/栈和队列/225.用队列实现栈.md +++ b/animation-simulation/栈和队列/225.用队列实现栈.md @@ -18,6 +18,7 @@ 下面我们来看一下题目代码,也是很容易理解。 +Java Code: ```java class MyStack { //初始化队列 @@ -55,3 +56,34 @@ class MyStack { ``` +JS Code: +```javascript +var MyStack = function() { + this.queue = []; +}; + +MyStack.prototype.push = function(x) { + this.queue.push(x); + if (this.queue.length > 1) { + let i = this.queue.length - 1; + while (i) { + this.queue.push(this.queue.shift()); + i--; + } + } +}; + +MyStack.prototype.pop = function() { + return this.queue.shift(); +}; + +MyStack.prototype.top = function() { + return this.empty() ? null : this.queue[0]; + +}; + +MyStack.prototype.empty = function() { + return !this.queue.length; +}; +``` + From 0b5744e2facc62de50cdf5ff8352adee8b0ecb0f Mon Sep 17 00:00:00 2001 From: daluozha <561890199@qq.com> Date: Tue, 27 Apr 2021 18:13:37 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=89=91=E6=8C=87offer09=20=20=E8=A1=A5?= =?UTF-8?q?=E5=85=85js=E7=89=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../栈和队列/剑指Offer09用两个栈实现队列.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md b/animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md index 5ac5c34..63d9c10 100644 --- a/animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md +++ b/animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md @@ -58,6 +58,7 @@ class CQueue { 大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。 +Java Code: ```java class CQueue { //初始化两个栈 @@ -89,3 +90,24 @@ class CQueue { } ``` +JS Code: +```javascript +var CQueue = function() { + this.stack1 = []; + this.stack2 = []; +}; + +CQueue.prototype.appendTail = function(value) { + this.stack1.push(value); +}; + +CQueue.prototype.deleteHead = function() { + if (!this.stack2.length) { + while(this.stack1.length) { + this.stack2.push(this.stack1.pop()); + } + } + return this.stack2.pop() || -1; +}; +``` + From 46b5f5eb7d12b5d3eea270c25830f3d45d6a9df7 Mon Sep 17 00:00:00 2001 From: daluozha <561890199@qq.com> Date: Tue, 27 Apr 2021 18:14:29 +0800 Subject: [PATCH 6/6] =?UTF-8?q?leetcode=20141=20=E8=A1=A5=E5=85=85js?= =?UTF-8?q?=E7=89=88=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../链表篇/leetcode141环形链表.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/animation-simulation/链表篇/leetcode141环形链表.md b/animation-simulation/链表篇/leetcode141环形链表.md index 528d421..d157030 100644 --- a/animation-simulation/链表篇/leetcode141环形链表.md +++ b/animation-simulation/链表篇/leetcode141环形链表.md @@ -38,6 +38,7 @@ **题目代码** +Java Code: ```java public class Solution { public boolean hasCycle(ListNode head) { @@ -56,3 +57,18 @@ public class Solution { } ``` +JS Code: +```javascript +var hasCycle = function(head) { + let fast = head; + let slow = head; + while (fast && fast.next) { + fast = fast.next.next; + slow = slow.next; + if (fast === slow) { + return true; + } + } + return false; +}; +```