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/2] =?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/2] =?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;