Skip to content

Commit 0312262

Browse files
authored
Merge branch 'master' into master
2 parents 8e76783 + e8d1a1a commit 0312262

File tree

66 files changed

+2614
-94
lines changed

Some content is hidden

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

66 files changed

+2614
-94
lines changed

problems/0027.移除元素.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
## 思路
3030

31+
[本题B站视频讲解](https://www.bilibili.com/video/BV12A4y1Z7LP)
32+
3133
有的同学可能说了,多余的元素,删掉不就得了。
3234

3335
**要知道数组的元素在内存地址中是连续的,不能单独删除数组中的某个元素,只能覆盖。**
@@ -75,10 +77,20 @@ public:
7577

7678
双指针法(快慢指针法): **通过一个快指针和慢指针在一个for循环下完成两个for循环的工作。**
7779

80+
定义快慢指针
81+
82+
* 快指针:寻找新数组的元素 ,新数组就是不含有目标元素的数组
83+
* 慢指针:指向更新 新数组下标的位置
84+
85+
很多同学这道题目做的很懵,就是不理解 快慢指针究竟都是什么含义,所以一定要明确含义,后面的思路就更容易理解了。
86+
7887
删除过程如下:
7988

8089
![27.移除元素-双指针法](https://tva1.sinaimg.cn/large/008eGmZEly1gntrds6r59g30du09mnpd.gif)
8190

91+
很多同学不了解
92+
93+
8294
**双指针法(快慢指针法)在数组和链表的操作中是非常常见的,很多考察数组、链表、字符串等操作的面试题,都使用双指针法。**
8395

8496
后续都会一一介绍到,本题代码如下:
@@ -104,8 +116,6 @@ public:
104116
* 时间复杂度:O(n)
105117
* 空间复杂度:O(1)
106118
107-
旧文链接:[数组:就移除个元素很难么?](https://programmercarl.com/0027.移除元素.html)
108-
109119
```CPP
110120
/**
111121
* 相向双指针方法,基于元素顺序可以改变的题目描述改变了元素相对位置,确保了移动最少元素
@@ -329,6 +339,17 @@ int removeElement(int* nums, int numsSize, int val){
329339
}
330340
```
331341
342+
Kotlin:
343+
```kotlin
344+
fun removeElement(nums: IntArray, `val`: Int): Int {
345+
var slowIndex = 0 // 初始化慢指针
346+
for (fastIndex in nums.indices) {
347+
if (nums[fastIndex] != `val`) nums[slowIndex++] = nums[fastIndex] // 在慢指针所在位置存储未被删除的元素
348+
}
349+
return slowIndex
350+
}
351+
```
352+
332353
Scala:
333354
```scala
334355
object Solution {
@@ -359,5 +380,6 @@ public class Solution {
359380
}
360381
}
361382
```
383+
362384
-----------------------
363385
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0035.搜索插入位置.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public:
7373
};
7474
```
7575
76-
* 时间复杂度:$O(n)$
77-
* 空间复杂度:$O(1)$
76+
* 时间复杂度:O(n)
77+
* 空间复杂度:O(1)
7878
7979
效率如下:
8080
@@ -135,14 +135,14 @@ public:
135135
// 目标值在数组所有元素之前 [0, -1]
136136
// 目标值等于数组中某一个元素 return middle;
137137
// 目标值插入数组中的位置 [left, right],return right + 1
138-
// 目标值在数组所有元素之后的情况 [left, right], return right + 1
138+
// 目标值在数组所有元素之后的情况 [left, right], 因为是右闭区间,所以 return right + 1
139139
return right + 1;
140140
}
141141
};
142142
```
143143

144-
* 时间复杂度:$O(\log n)$
145-
* 时间复杂度:$O(1)$
144+
* 时间复杂度:O(log n)
145+
* 时间复杂度:O(1)
146146

147147
效率如下:
148148
![35_搜索插入位置2](https://img-blog.csdnimg.cn/2020121623272877.png)
@@ -178,7 +178,7 @@ public:
178178
// 目标值在数组所有元素之前 [0,0)
179179
// 目标值等于数组中某一个元素 return middle
180180
// 目标值插入数组中的位置 [left, right) ,return right 即可
181-
// 目标值在数组所有元素之后的情况 [left, right),return right 即可
181+
// 目标值在数组所有元素之后的情况 [left, right),因为是右开区间,所以 return right
182182
return right;
183183
}
184184
};

problems/0042.接雨水.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,91 @@ var trap = function(height) {
744744
};
745745
```
746746

747+
### TypeScript
748+
749+
双指针法:
750+
751+
```typescript
752+
function trap(height: number[]): number {
753+
const length: number = height.length;
754+
let resVal: number = 0;
755+
for (let i = 0; i < length; i++) {
756+
let leftMaxHeight: number = height[i],
757+
rightMaxHeight: number = height[i];
758+
let leftIndex: number = i - 1,
759+
rightIndex: number = i + 1;
760+
while (leftIndex >= 0) {
761+
if (height[leftIndex] > leftMaxHeight)
762+
leftMaxHeight = height[leftIndex];
763+
leftIndex--;
764+
}
765+
while (rightIndex < length) {
766+
if (height[rightIndex] > rightMaxHeight)
767+
rightMaxHeight = height[rightIndex];
768+
rightIndex++;
769+
}
770+
resVal += Math.min(leftMaxHeight, rightMaxHeight) - height[i];
771+
}
772+
return resVal;
773+
};
774+
```
775+
776+
动态规划:
777+
778+
```typescript
779+
function trap(height: number[]): number {
780+
const length: number = height.length;
781+
const leftMaxHeightDp: number[] = [],
782+
rightMaxHeightDp: number[] = [];
783+
leftMaxHeightDp[0] = height[0];
784+
rightMaxHeightDp[length - 1] = height[length - 1];
785+
for (let i = 1; i < length; i++) {
786+
leftMaxHeightDp[i] = Math.max(height[i], leftMaxHeightDp[i - 1]);
787+
}
788+
for (let i = length - 2; i >= 0; i--) {
789+
rightMaxHeightDp[i] = Math.max(height[i], rightMaxHeightDp[i + 1]);
790+
}
791+
let resVal: number = 0;
792+
for (let i = 0; i < length; i++) {
793+
resVal += Math.min(leftMaxHeightDp[i], rightMaxHeightDp[i]) - height[i];
794+
}
795+
return resVal;
796+
};
797+
```
798+
799+
单调栈:
800+
801+
```typescript
802+
function trap(height: number[]): number {
803+
const length: number = height.length;
804+
const stack: number[] = [];
805+
stack.push(0);
806+
let resVal: number = 0;
807+
for (let i = 1; i < length; i++) {
808+
let top = stack[stack.length - 1];
809+
if (height[top] > height[i]) {
810+
stack.push(i);
811+
} else if (height[top] === height[i]) {
812+
stack.pop();
813+
stack.push(i);
814+
} else {
815+
while (stack.length > 0 && height[top] < height[i]) {
816+
let mid = stack.pop();
817+
if (stack.length > 0) {
818+
let left = stack[stack.length - 1];
819+
let h = Math.min(height[left], height[i]) - height[mid];
820+
let w = i - left - 1;
821+
resVal += h * w;
822+
top = stack[stack.length - 1];
823+
}
824+
}
825+
stack.push(i);
826+
}
827+
}
828+
return resVal;
829+
};
830+
```
831+
747832
### C:
748833

749834
一种更简便的双指针方法:

problems/0046.全排列.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ function permute(nums: number[]): number[][] {
341341
return resArr;
342342
function backTracking(nums: number[], route: number[]): void {
343343
if (route.length === nums.length) {
344-
resArr.push(route.slice());
344+
resArr.push([...route]);
345345
return;
346346
}
347347
let tempVal: number;

problems/0047.全排列II.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ var permuteUnique = function (nums) {
268268

269269
function backtracing( used) {
270270
if (path.length === nums.length) {
271-
result.push(path.slice())
271+
result.push([...path])
272272
return
273273
}
274274
for (let i = 0; i < nums.length; i++) {
@@ -303,7 +303,7 @@ function permuteUnique(nums: number[]): number[][] {
303303
return resArr;
304304
function backTracking(nums: number[], route: number[]): void {
305305
if (route.length === nums.length) {
306-
resArr.push(route.slice());
306+
resArr.push([...route]);
307307
return;
308308
}
309309
for (let i = 0, length = nums.length; i < length; i++) {

problems/0052.N皇后II.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并
4444
# 思路
4545

4646

47-
想看[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别
47+
详看[51.N皇后](https://mp.weixin.qq.com/s/lU_QwCMj6g60nh8m98GAWg) ,基本没有区别
4848

4949
# C++代码
5050

problems/0053.最大子序和(动态规划).md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,24 @@ const maxSubArray = nums => {
186186
};
187187
```
188188

189+
TypeScript:
190+
191+
```typescript
192+
function maxSubArray(nums: number[]): number {
193+
/**
194+
dp[i]:以nums[i]结尾的最大和
195+
*/
196+
const dp: number[] = []
197+
dp[0] = nums[0];
198+
let resMax: number = 0;
199+
for (let i = 1; i < nums.length; i++) {
200+
dp[i] = Math.max(dp[i - 1] + nums[i], nums[i]);
201+
resMax = Math.max(resMax, dp[i]);
202+
}
203+
return resMax;
204+
};
205+
```
206+
189207

190208

191209
-----------------------

problems/0063.不同路径II.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ dp[i][j] :表示从(0 ,0)出发,到(i, j) 有dp[i][j]条不同的路
6666

6767
所以代码为:
6868

69-
```
69+
```cpp
7070
if (obstacleGrid[i][j] == 0) { // 当(i, j)没有障碍的时候,再推导dp[i][j]
7171
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
7272
}
@@ -76,7 +76,7 @@ if (obstacleGrid[i][j] == 0) { // 当(i, j)没有障碍的时候,再推导dp[i
7676

7777
[62.不同路径](https://programmercarl.com/0062.不同路径.html)不同路径中我们给出如下的初始化:
7878

79-
```
79+
```cpp
8080
vector<vector<int>> dp(m, vector<int>(n, 0)); // 初始值为0
8181
for (int i = 0; i < m; i++) dp[i][0] = 1;
8282
for (int j = 0; j < n; j++) dp[0][j] = 1;
@@ -138,6 +138,8 @@ public:
138138
int uniquePathsWithObstacles(vector<vector<int>>& obstacleGrid) {
139139
int m = obstacleGrid.size();
140140
int n = obstacleGrid[0].size();
141+
if (obstacleGrid[m - 1][n - 1] == 1 || obstacleGrid[0][0] == 1) //如果在起点或终点出现了障碍,直接返回0
142+
return 0;
141143
vector<vector<int>> dp(m, vector<int>(n, 0));
142144
for (int i = 0; i < m && obstacleGrid[i][0] == 0; i++) dp[i][0] = 1;
143145
for (int j = 0; j < n && obstacleGrid[0][j] == 0; j++) dp[0][j] = 1;

problems/0072.编辑距离.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,5 +327,42 @@ const minDistance = (word1, word2) => {
327327
};
328328
```
329329

330+
TypeScript:
331+
332+
```typescript
333+
function minDistance(word1: string, word2: string): number {
334+
/**
335+
dp[i][j]: word1前i个字符,word2前j个字符,最少操作数
336+
dp[0][0]=0:表示word1前0个字符为'', word2前0个字符为''
337+
*/
338+
const length1: number = word1.length,
339+
length2: number = word2.length;
340+
const dp: number[][] = new Array(length1 + 1).fill(0)
341+
.map(_ => new Array(length2 + 1).fill(0));
342+
for (let i = 0; i <= length1; i++) {
343+
dp[i][0] = i;
344+
}
345+
for (let i = 0; i <= length2; i++) {
346+
dp[0][i] = i;
347+
}
348+
for (let i = 1; i <= length1; i++) {
349+
for (let j = 1; j <= length2; j++) {
350+
if (word1[i - 1] === word2[j - 1]) {
351+
dp[i][j] = dp[i - 1][j - 1];
352+
} else {
353+
dp[i][j] = Math.min(
354+
dp[i - 1][j],
355+
dp[i][j - 1],
356+
dp[i - 1][j - 1]
357+
) + 1;
358+
}
359+
}
360+
}
361+
return dp[length1][length2];
362+
};
363+
```
364+
365+
366+
330367
-----------------------
331368
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0078.子集.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ var subsets = function(nums) {
260260
let result = []
261261
let path = []
262262
function backtracking(startIndex) {
263-
result.push(path.slice())
263+
result.push([...path])
264264
for(let i = startIndex; i < nums.length; i++) {
265265
path.push(nums[i])
266266
backtracking(i + 1)
@@ -280,7 +280,7 @@ function subsets(nums: number[]): number[][] {
280280
backTracking(nums, 0, []);
281281
return resArr;
282282
function backTracking(nums: number[], startIndex: number, route: number[]): void {
283-
resArr.push(route.slice());
283+
resArr.push([...route]);
284284
let length = nums.length;
285285
if (startIndex === length) return;
286286
for (let i = startIndex; i < length; i++) {

0 commit comments

Comments
 (0)