Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add solutions to lc/lcof2 problems #635

Merged
merged 2 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lcof2/剑指 Offer II 010. 和为 k 的子数组/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,31 @@ func subarraySum(nums []int, k int) int {
}
```

### **C++**

```cpp
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
if (nums.size() < 0) return 0;

int presum = 0;
int count = 0;
unordered_map<int, int> mp;
mp[0] = 1;

for (int right = 0; right < nums.size(); right++) {
presum += nums[right];
count += mp[presum - k];
mp[presum]++;
}

return count;
}
};
```


### **...**

```
Expand Down
19 changes: 19 additions & 0 deletions lcof2/剑指 Offer II 010. 和为 k 的子数组/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
if (nums.size() < 0) return 0;

int presum = 0;
int count = 0;
unordered_map<int, int> mp;
mp[0] = 1;

for (int right = 0; right < nums.size(); right++) {
presum += nums[right];
count += mp[presum - k];
mp[presum]++;
}

return count;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,30 @@ func max(a, b int) int {
}
```

### **C++**

```cpp
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int presum = 0;
int maxlen = 0;
unordered_map<int, int> mp;
mp[0] = -1;
for (int i = 0; i < nums.size(); i++) {
presum += nums[i] == 0? -1: 1;
if (mp.find(presum) != mp.end())
maxlen = max(maxlen, i - mp[presum]);
else
mp[presum] = i;
}

return maxlen;
}
};
```


### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int presum = 0;
int maxlen = 0;
unordered_map<int, int> mp;
mp[0] = -1;
for (int i = 0; i < nums.size(); i++) {
presum += nums[i] == 0? -1: 1;
if (mp.find(presum) != mp.end())
maxlen = max(maxlen, i - mp[presum]);
else
mp[presum] = i;
}

return maxlen;
}
};
24 changes: 24 additions & 0 deletions solution/0500-0599/0525.Contiguous Array/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@

```

### **C++**

```cpp
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int presum = 0;
int maxlen = 0;
unordered_map<int, int> mp;
mp[0] = -1;
for (int i = 0; i < nums.size(); i++) {
presum += nums[i] == 0? -1: 1;
if (mp.find(presum) != mp.end())
maxlen = max(maxlen, i - mp[presum]);
else
mp[presum] = i;
}

return maxlen;
}
};
```


### **...**

```
Expand Down
23 changes: 23 additions & 0 deletions solution/0500-0599/0525.Contiguous Array/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,29 @@ The length of the given binary array will not exceed 50,000.

```

### **C++**

```cpp
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int presum = 0;
int maxlen = 0;
unordered_map<int, int> mp;
mp[0] = -1;
for (int i = 0; i < nums.size(); i++) {
presum += nums[i] == 0? -1: 1;
if (mp.find(presum) != mp.end())
maxlen = max(maxlen, i - mp[presum]);
else
mp[presum] = i;
}

return maxlen;
}
};
```

### **...**

```
Expand Down
18 changes: 18 additions & 0 deletions solution/0500-0599/0525.Contiguous Array/Solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
class Solution {
public:
int findMaxLength(vector<int>& nums) {
int presum = 0;
int maxlen = 0;
unordered_map<int, int> mp;
mp[0] = -1;
for (int i = 0; i < nums.size(); i++) {
presum += nums[i] == 0? -1: 1;
if (mp.find(presum) != mp.end())
maxlen = max(maxlen, i - mp[presum]);
else
mp[presum] = i;
}

return maxlen;
}
};