Skip to content

Commit 212a9bb

Browse files
committed
add solution
1 parent 143c2e9 commit 212a9bb

File tree

179 files changed

+3501
-2760
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+3501
-2760
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ collect some good articles about data structure and algorithm.
1919

2020
读者可以根据自己的需要阅读,希望能够对您有所帮助,有何不足之处请多指正!
2121

22-
[我的力扣主页](https://leetcode-cn.com/u/muyids/)
22+
[我的力扣主页](https://leetcode.cn/u/muyids/)
2323

2424
访问[我的电子书《从零开始学算法》](https://muyids.github.io/alg/),获取更好的阅读体验
2525

@@ -31,8 +31,8 @@ collect some good articles about data structure and algorithm.
3131

3232
## 🙉 说明
3333

34-
* 本项目题目来源:[力扣(LeetCode)](https://leetcode-cn.com)
35-
* 题目著作权归[力扣(LeetCode)](https://leetcode-cn.com) 所有。商业转载请联系官方授权,非商业转载请注明出处。
34+
* 本项目题目来源:[力扣(LeetCode)](https://leetcode.cn)
35+
* 题目著作权归[力扣(LeetCode)](https://leetcode.cn) 所有。商业转载请联系官方授权,非商业转载请注明出处。
3636
* 题解语言不限,专注于算法实现
3737

3838
## 🌲目录
@@ -60,7 +60,7 @@ node script deploy.sh
6060

6161
## 🔐 Problems & Solutions
6262

63-
完成进度([1022](./TOC-By-ID.md)🔑/ [2633](https://leetcode-cn.com/problemset/all/)🔒)
63+
完成进度([1036](./TOC-By-ID.md)🔑/ [2684](https://leetcode.cn/problemset/all/)🔒)
6464

6565
- 🔗 [标签查找](./TOC-By-Tag.md)
6666

TOC-By-ID.md

Lines changed: 562 additions & 555 deletions
Large diffs are not rendered by default.

TOC-By-Tag.md

Lines changed: 1854 additions & 1841 deletions
Large diffs are not rendered by default.

algorithms/1-100/22.generate-parentheses.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#### [剑指 Offer II 085. 生成匹配的括号](https://leetcode-cn.com/problems/IDBivT/)
1+
#### [剑指 Offer II 085. 生成匹配的括号](https://leetcode.cn/problems/IDBivT/)
22

33

44

algorithms/1-100/30.substring-with-concatenation-of-all-words.md

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
---
2323

2424

25-
### 滑动窗口
25+
# 滑动窗口
2626

2727
- 记录窗口中单词出现次数
28-
- l, r两个指针维护长度为 words.size() * words[0].size() 的窗口
28+
- $l, r$两个指针维护长度为 $words.size() * words[0].size()$的窗口
2929
- 单词长度都相同,每次两个指针右移一样的距离
30-
- 外层遍历指针起始位置 0 -> words[0].size()
30+
- 外层遍历指针起始位置 $0 -> words[0].size()$
3131

3232
关键点:
3333

@@ -37,7 +37,13 @@
3737
2. 当前位置单词出现次数超过字典中总次数
3838
3. 滑动窗口中单词个数等于字典中单词总个数
3939

40-
### 代码
40+
## 代码实现
41+
42+
43+
44+
45+
46+
### C++
4147

4248
```cpp
4349

@@ -73,3 +79,45 @@ public:
7379
}
7480
};
7581
```
82+
83+
84+
85+
86+
87+
### java
88+
89+
90+
91+
```java
92+
class Solution {
93+
public List<Integer> findSubstring(String s, String[] words) {
94+
List<Integer> res = new ArrayList<>();
95+
int d = words[0].length();
96+
for (int start = 0; start < d; start++) {
97+
int l = start, r = start;
98+
Map<String, Integer> cnt = new HashMap<>();
99+
for (String w: words){
100+
cnt.put(w, cnt.getOrDefault(w, 0) + 1);
101+
}
102+
while (r + d <= s.length()) {
103+
String ns = s.substring(r, r+d);
104+
r += d;
105+
106+
if (cnt.containsKey(ns)) {
107+
cnt.put(ns, cnt.get(ns) - 1);
108+
}
109+
while(l < r && (!cnt.containsKey(ns) || cnt.get(ns) < 0)){
110+
String ls = s.substring(l, l+d);
111+
if (cnt.containsKey(ls)) cnt.put(ls, cnt.get(ls) + 1);
112+
l+=d;
113+
}
114+
115+
if (r - l == d * words.length) res.add(l);
116+
}
117+
}
118+
119+
return res;
120+
}
121+
}
122+
```
123+

algorithms/1-100/43.multiply-strings.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ num1 和 num2 均不以零开头,除非是数字 0 本身。
1616
不能使用任何标准库的大数类型(比如 BigInteger)或直接将输入转换为整数来处理。
1717

1818
来源:力扣(LeetCode)
19-
链接:https://leetcode-cn.com/problems/multiply-strings
19+
链接:https://leetcode.cn/problems/multiply-strings
2020
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2121

2222
----

algorithms/1-100/52.n-queens-ii.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,
33
给定一个整数 n,返回 n 皇后不同的解决方案的数量。
44

55
来源:力扣(LeetCode)
6-
链接:https://leetcode-cn.com/problems/n-queens-ii
6+
链接:https://leetcode.cn/problems/n-queens-ii
77
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
88

99
----

algorithms/1-100/53.maximum-subarray.md

Lines changed: 91 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,85 @@
1111

1212
---
1313

14-
## 暴力求解
14+
## 暴力求解 $O(N^2)$
1515

16-
枚举所有起点和终点
16+
枚举所有起点和终点,求和取最大
1717

18-
O(N^2)
18+
```java
19+
class Solution {
20+
public int maxSubArray(int[] nums) {
21+
int n = nums.length;
22+
int res = nums[0];
23+
for (int i =0; i< n; i++){
24+
int sum = nums[i];
25+
res = Math.max(sum, res);
26+
for (int j = i + 1; j < n; j++){
27+
sum += nums[j];
28+
res = Math.max(sum, res);
29+
}
30+
}
31+
return res;
32+
}
33+
}
34+
```
35+
36+
提示:
37+
38+
- $ 1 <= nums.length <= 10^5 $
39+
40+
O(N^2) 会 TLE
1941

20-
## 分治
42+
43+
## 动态规划 $O(n)$
44+
45+
- `f[i]` 表示所有以i结尾的子数组的最大值
46+
- 状态计算:$ f(i) = max(f(i-1) + nums[i], nums[i]) $
47+
48+
### 代码实现
49+
50+
```java
51+
class Solution {
52+
public int maxSubArray(int[] nums) {
53+
int n = nums.length;
54+
int f[] = new int[n];
55+
f[0] = nums[0];
56+
for (int i = 1; i < n; i++){
57+
f[i] = Math.max(nums[i], f[i-1] + nums[i]);
58+
}
59+
return Arrays.stream(f).max().getAsInt();
60+
}
61+
}
62+
```
63+
64+
优化空间,在原数组上修改:
2165

2266
```cpp
2367
class Solution {
68+
public:
69+
int maxSubArray(vector<int>& nums) {
70+
int n = nums.size();
71+
for (int i = 1; i < n; i++){
72+
nums[i] = max(nums[i] , nums[i] + nums[i-1]);
73+
}
74+
return *max_element(nums.begin(), nums.end());
75+
}
76+
};
77+
```
78+
79+
## 分治
80+
81+
最大子序和只可能来自于三个 子区间:
82+
83+
- $ [left, mid] $
84+
- $ [mid+1, right] $
85+
- $ (..., mid, mid+1, ...) $,即 一定包含 mid, mid+1 的子区间
86+
87+
88+
```cpp []
89+
class Solution {
2490
public:
2591
int calc(int l, int r, vector<int>& nums) {
26-
if (l == r)
27-
return nums[l];
92+
if (l == r) return nums[l];
2893
int mid = (l + r) >> 1;
2994
int lmax = nums[mid], lsum = 0, rmax = nums[mid + 1], rsum = 0;
3095
for (int i = mid; i >= l; i--) {
@@ -45,34 +110,31 @@ public:
45110
};
46111
```
47112

48-
## 动态规划O(n)
49-
50-
dp问题思考方式
51-
52-
- 状态表示`f[i]`
53-
- 所有以i结尾的子数组
54-
- 属性:Max/Min/数量 `f[i]`的最大值
55-
- 状态计算
56-
- `f(i) = Math.max(f(i-1)+ nums[i], 0)`
113+
```java []
114+
class Solution {
57115

116+
int divide(int l , int r , int[] nums){
117+
if (l == r) return nums[l];
118+
int mid = (l + r) >> 1;
58119

120+
int lmax = nums[mid], lsum = 0, rmax = nums[mid+1], rsum = 0;
59121

60-
### 代码实现
61-
62-
```cpp
63-
class Solution {
64-
public:
65-
int maxSubArray(vector<int>& nums) {
66-
int n = nums.size();
67-
for (int i = 1; i < n; i++){
68-
nums[i] = max(nums[i] , nums[i] + nums[i-1]);
122+
for (int i = mid; i>= l; i--){
123+
lsum += nums[i];
124+
lmax = Math.max(lmax, lsum);
125+
}
126+
for (int i = mid+1; i <= r; i++){
127+
rsum += nums[i];
128+
rmax = Math.max(rmax, rsum);
69129
}
70-
return *max_element(nums.begin(), nums.end());
130+
return Math.max(lmax+rmax, Math.max(divide(l , mid, nums), divide(mid+1, r, nums)));
71131
}
72-
};
73-
```
74-
75-
76132

133+
public int maxSubArray(int[] nums) {
134+
int n = nums.length;
135+
return divide(0, n-1, nums);
136+
}
137+
}
138+
```
77139

78140

algorithms/1-100/55.jump-game.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
解释: 无论怎样,你总会到达索引为 3 的位置。但该位置的最大跳跃长度是 0 , 所以你永远不可能到达最后一个位置。
1717

1818
来源:力扣(LeetCode)
19-
链接:https://leetcode-cn.com/problems/jump-game
19+
链接:https://leetcode.cn/problems/jump-game
2020
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
2121

2222
----

algorithms/1-100/58.length-of-last-word.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
来源:力扣(LeetCode)
16-
链接:https://leetcode-cn.com/problems/length-of-last-word
16+
链接:https://leetcode.cn/problems/length-of-last-word
1717
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
1818

1919

0 commit comments

Comments
 (0)