Skip to content

Commit 6b85bbd

Browse files
Merge pull request youngyangyang04#1000 from bqlin/master
补充 Swift 实现,修改错别字
2 parents d236296 + 1104815 commit 6b85bbd

13 files changed

+42
-49
lines changed

problems/0001.两数之和.md

+8-10
Original file line numberDiff line numberDiff line change
@@ -207,18 +207,16 @@ function twoSum(array $nums, int $target): array
207207
Swift:
208208
```swift
209209
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
210-
var res = [Int]()
211-
var dict = [Int : Int]()
212-
for i in 0 ..< nums.count {
213-
let other = target - nums[i]
214-
if dict.keys.contains(other) {
215-
res.append(i)
216-
res.append(dict[other]!)
217-
return res
210+
// 值: 下标
211+
var map = [Int: Int]()
212+
for (i, e) in nums.enumerated() {
213+
if let v = map[target - e] {
214+
return [v, i]
215+
} else {
216+
map[e] = i
218217
}
219-
dict[nums[i]] = i
220218
}
221-
return res
219+
return []
222220
}
223221
```
224222

problems/0031.下一个排列.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public:
8686
}
8787
}
8888
}
89-
// 到这里了说明整个数组都是倒叙了,反转一下便可
89+
// 到这里了说明整个数组都是倒序了,反转一下便可
9090
reverse(nums.begin(), nums.end());
9191
}
9292
};

problems/0151.翻转字符串里的单词.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
不能使用辅助空间之后,那么只能在原字符串上下功夫了。
4242

43-
想一下,我们将整个字符串都反转过来,那么单词的顺序指定是倒序了,只不过单词本身也倒叙了,那么再把单词反转一下,单词不就正过来了。
43+
想一下,我们将整个字符串都反转过来,那么单词的顺序指定是倒序了,只不过单词本身也倒序了,那么再把单词反转一下,单词不就正过来了。
4444

4545
所以解题思路如下:
4646

problems/0344.反转字符串.md

-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ s[j] = tmp;
101101
s[i] ^= s[j];
102102
s[j] ^= s[i];
103103
s[i] ^= s[j];
104-
105104
```
106105

107106
这道题目还是比较简单的,但是我正好可以通过这道题目说一说在刷题的时候,使用库函数的原则。

problems/0347.前K个高频元素.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public:
107107
}
108108
}
109109

110-
// 找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒叙来输出到数组
110+
// 找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒序来输出到数组
111111
vector<int> result(k);
112112
for (int i = k - 1; i >= 0; i--) {
113113
result[i] = pri_que.top().first;
@@ -180,7 +180,7 @@ class Solution:
180180
if len(pri_que) > k: #如果堆的大小大于了K,则队列弹出,保证堆的大小一直为k
181181
heapq.heappop(pri_que)
182182

183-
#找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒叙来输出到数组
183+
#找出前K个高频元素,因为小顶堆先弹出的是最小的,所以倒序来输出到数组
184184
result = [0] * k
185185
for i in range(k-1, -1, -1):
186186
result[i] = heapq.heappop(pri_que)[1]

problems/0416.分割等和子集.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ vector<int> dp(10001, 0);
112112
113113
4. 确定遍历顺序
114114
115-
在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒叙遍历
115+
在[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒序遍历
116116
117117
代码如下:
118118

problems/0454.四数相加II.md

+16-20
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ D = [ 0, 2]
4444

4545
1. 首先定义 一个unordered_map,key放a和b两数之和,value 放a和b两数之和出现的次数。
4646
2. 遍历大A和大B数组,统计两个数组元素之和,和出现的次数,放到map中。
47-
3. 定义int变量count,用来统计a+b+c+d = 0 出现的次数。
47+
3. 定义int变量count,用来统计 a+b+c+d = 0 出现的次数。
4848
4. 在遍历大C和大D数组,找到如果 0-(c+d) 在map中出现过的话,就用count把map中key对应的value也就是出现次数统计出来。
4949
5. 最后返回统计值 count 就可以了
5050

@@ -139,7 +139,7 @@ class Solution(object):
139139
return count
140140

141141

142-
```
142+
```
143143

144144
Go:
145145
```go
@@ -229,28 +229,24 @@ class Solution {
229229
Swift:
230230
```swift
231231
func fourSumCount(_ nums1: [Int], _ nums2: [Int], _ nums3: [Int], _ nums4: [Int]) -> Int {
232-
// key:a+b的数值,value:a+b数值出现的次数
233-
var map = [Int: Int]()
234-
// 遍历nums1和nums2数组,统计两个数组元素之和,和出现的次数,放到map中
235-
for i in 0 ..< nums1.count {
236-
for j in 0 ..< nums2.count {
237-
let sum1 = nums1[i] + nums2[j]
238-
map[sum1] = (map[sum1] ?? 0) + 1
232+
// ab和: ab和出现次数
233+
var countDic = [Int: Int]()
234+
for a in nums1 {
235+
for b in nums2 {
236+
let key = a + b
237+
countDic[key] = countDic[key, default: 0] + 1
239238
}
240239
}
241-
// 统计a+b+c+d = 0 出现的次数
242-
var res = 0
243-
// 在遍历大num3和num4数组,找到如果 0-(c+d) 在map中出现过的话,就把map中key对应的value也就是出现次数统计出来。
244-
for i in 0 ..< nums3.count {
245-
for j in 0 ..< nums4.count {
246-
let sum2 = nums3[i] + nums4[j]
247-
let other = 0 - sum2
248-
if map.keys.contains(other) {
249-
res += map[other]!
250-
}
240+
241+
// 通过-(c + d)作为key,去累加ab和出现的次数
242+
var result = 0
243+
for c in nums3 {
244+
for d in nums4 {
245+
let key = -(c + d)
246+
result += countDic[key, default: 0]
251247
}
252248
}
253-
return res
249+
return result
254250
}
255251
```
256252

problems/0685.冗余连接II.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ for (int i = 0; i < n; i++) {
6868

6969
```cpp
7070
vector<int> vec; // 记录入度为2的边(如果有的话就两条边)
71-
// 找入度为2的节点所对应的边,注意要倒叙,因为优先返回最后出现在二维数组中的答案
71+
// 找入度为2的节点所对应的边,注意要倒序,因为优先返回最后出现在二维数组中的答案
7272
for (int i = n - 1; i >= 0; i--) {
7373
if (inDegree[edges[i][1]] == 2) {
7474
vec.push_back(i);
@@ -577,7 +577,7 @@ var findRedundantDirectedConnection = function(edges) {
577577
inDegree[edges[i][1]]++; // 统计入度
578578
}
579579
let vec = [];// 记录入度为2的边(如果有的话就两条边)
580-
// 找入度为2的节点所对应的边,注意要倒叙,因为优先返回最后出现在二维数组中的答案
580+
// 找入度为2的节点所对应的边,注意要倒序,因为优先返回最后出现在二维数组中的答案
581581
for (let i = n - 1; i >= 0; i--) {
582582
if (inDegree[edges[i][1]] == 2) {
583583
vec.push(i);

problems/0746.使用最小花费爬楼梯.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ dp[1] = cost[1];
8282

8383
**但是稍稍有点难度的动态规划,其遍历顺序并不容易确定下来**
8484

85-
例如:01背包,都知道两个for循环,一个for遍历物品嵌套一个for遍历背包容量,那么为什么不是一个for遍历背包容量嵌套一个for遍历物品呢? 以及在使用一维dp数组的时候遍历背包容量为什么要倒叙呢
85+
例如:01背包,都知道两个for循环,一个for遍历物品嵌套一个for遍历背包容量,那么为什么不是一个for遍历背包容量嵌套一个for遍历物品呢? 以及在使用一维dp数组的时候遍历背包容量为什么要倒序呢
8686

8787
**这些都是遍历顺序息息相关。当然背包问题后续「代码随想录」都会重点讲解的!**
8888

problems/1002.查找常用字符.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ words[i] 由小写英文字母组成
5858

5959
先统计第一个字符串所有字符出现的次数,代码如下:
6060

61-
```
61+
```cpp
6262
int hash[26] = {0}; // 用来统计所有字符串里字符出现的最小频率
6363
for (int i = 0; i < A[0].size(); i++) { // 用第一个字符串给hash初始化
6464
hash[A[0][i] - 'a']++;
@@ -71,7 +71,7 @@ for (int i = 0; i < A[0].size(); i++) { // 用第一个字符串给hash初始化
7171

7272
代码如下:
7373

74-
```
74+
```cpp
7575
int hashOtherStr[26] = {0}; // 统计除第一个字符串外字符的出现频率
7676
for (int i = 1; i < A.size(); i++) {
7777
memset(hashOtherStr, 0, 26 * sizeof(int));
@@ -84,11 +84,11 @@ for (int i = 1; i < A.size(); i++) {
8484
}
8585
}
8686
```
87-
此时hash里统计着字符在所有字符串里出现的最小次数,那么把hash转正题目要求的输出格式就可以了
87+
此时hash里统计着字符在所有字符串里出现的最小次数,那么把hash转成题目要求的输出格式就可以了
8888

8989
代码如下:
9090

91-
```
91+
```cpp
9292
// 将hash统计的字符次数,转成输出形式
9393
for (int i = 0; i < 26; i++) {
9494
while (hash[i] != 0) { // 注意这里是while,多个重复的字符

problems/1047.删除字符串中的所有相邻重复项.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
![1047.删除字符串中的所有相邻重复项](https://code-thinking.cdn.bcebos.com/gifs/1047.删除字符串中的所有相邻重复项.gif)
6565

66-
从栈中弹出剩余元素,此时是字符串ac,因为从栈里弹出的元素是倒叙的,所以在对字符串进行反转一下,就得到了最终的结果。
66+
从栈中弹出剩余元素,此时是字符串ac,因为从栈里弹出的元素是倒序的,所以在对字符串进行反转一下,就得到了最终的结果。
6767

6868
C++代码 :
6969

problems/1049.最后一块石头的重量II.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ vector<int> dp(15001, 0);
8787
4. 确定遍历顺序
8888

8989

90-
[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒叙遍历
90+
[动态规划:关于01背包问题,你该了解这些!(滚动数组)](https://programmercarl.com/背包理论基础01背包-2.html)中就已经说明:如果使用一维dp数组,物品遍历的for循环放在外层,遍历背包的for循环放在内层,且内层for循环倒序遍历
9191

9292
代码如下:
9393

problems/背包理论基础01背包-2.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ for(int i = 0; i < weight.size(); i++) { // 遍历物品
103103

104104
为什么呢?
105105

106-
**倒叙遍历是为了保证物品i只被放入一次**。但如果一旦正序遍历了,那么物品0就会被重复加入多次!
106+
**倒序遍历是为了保证物品i只被放入一次**。但如果一旦正序遍历了,那么物品0就会被重复加入多次!
107107

108108
举一个例子:物品0的重量weight[0] = 1,价值value[0] = 15
109109

@@ -115,17 +115,17 @@ dp[2] = dp[2 - weight[0]] + value[0] = 30
115115

116116
此时dp[2]就已经是30了,意味着物品0,被放入了两次,所以不能正序遍历。
117117

118-
为什么倒叙遍历,就可以保证物品只放入一次呢?
118+
为什么倒序遍历,就可以保证物品只放入一次呢?
119119

120-
倒叙就是先算dp[2]
120+
倒序就是先算dp[2]
121121

122122
dp[2] = dp[2 - weight[0]] + value[0] = 15 (dp数组已经都初始化为0)
123123

124124
dp[1] = dp[1 - weight[0]] + value[0] = 15
125125

126126
所以从后往前循环,每次取得状态不会和之前取得状态重合,这样每种物品就只取一次了。
127127

128-
**那么问题又来了,为什么二维dp数组历的时候不用倒叙呢**
128+
**那么问题又来了,为什么二维dp数组历的时候不用倒序呢**
129129

130130
因为对于二维dp,dp[i][j]都是通过上一层即dp[i - 1][j]计算而来,本层的dp[i][j]并不会被覆盖!
131131

0 commit comments

Comments
 (0)