Skip to content

Commit 168b0ba

Browse files
Merge branch 'master' of github.com:youngyangyang04/leetcode-master
2 parents ce28066 + 82857ee commit 168b0ba

15 files changed

+348
-9
lines changed

problems/0027.移除元素.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,8 @@ func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
281281

282282
for fastIndex in 0..<nums.count {
283283
if val != nums[fastIndex] {
284-
if slowIndex != fastIndex {
285284
nums[slowIndex] = nums[fastIndex]
286-
}
287-
slowIndex += 1
285+
slowIndex += 1
288286
}
289287
}
290288
return slowIndex

problems/0045.跳跃游戏II.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,27 @@ var jump = function(nums) {
250250
};
251251
```
252252

253+
### TypeScript
254+
255+
```typescript
256+
function jump(nums: number[]): number {
257+
const length: number = nums.length;
258+
let curFarthestIndex: number = 0,
259+
nextFarthestIndex: number = 0;
260+
let curIndex: number = 0;
261+
let stepNum: number = 0;
262+
while (curIndex < length - 1) {
263+
nextFarthestIndex = Math.max(nextFarthestIndex, curIndex + nums[curIndex]);
264+
if (curIndex === curFarthestIndex) {
265+
curFarthestIndex = nextFarthestIndex;
266+
stepNum++;
267+
}
268+
curIndex++;
269+
}
270+
return stepNum;
271+
};
272+
```
273+
253274

254275

255276

problems/0055.跳跃游戏.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,23 @@ var canJump = function(nums) {
154154
};
155155
```
156156

157+
### TypeScript
158+
159+
```typescript
160+
function canJump(nums: number[]): boolean {
161+
let farthestIndex: number = 0;
162+
let cur: number = 0;
163+
while (cur <= farthestIndex) {
164+
farthestIndex = Math.max(farthestIndex, cur + nums[cur]);
165+
if (farthestIndex >= nums.length - 1) return true;
166+
cur++;
167+
}
168+
return false;
169+
};
170+
```
171+
172+
173+
157174

158175
-----------------------
159176
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

problems/0059.螺旋矩阵II.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ var generateMatrix = function(n) {
246246
res[row][col] = count++;
247247
}
248248
// 下行从右到左(左闭右开)
249-
for (; col > startX; col--) {
249+
for (; col > startY; col--) {
250250
res[row][col] = count++;
251251
}
252252
// 左列做下到上(左闭右开)
253-
for (; row > startY; row--) {
253+
for (; row > startX; row--) {
254254
res[row][col] = count++;
255255
}
256256

problems/0121.买卖股票的最佳时机.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,36 @@ class Solution:
311311
```
312312

313313
Go:
314+
> 贪心法:
315+
```Go
316+
func maxProfit(prices []int) int {
317+
low := math.MaxInt32
318+
rlt := 0
319+
for i := range prices{
320+
low = min(low, prices[i])
321+
rlt = max(rlt, prices[i]-low)
322+
}
323+
324+
return rlt
325+
}
326+
func min(a, b int) int {
327+
if a < b{
328+
return a
329+
}
330+
331+
return b
332+
}
333+
334+
func max(a, b int) int {
335+
if a > b{
336+
return a
337+
}
338+
339+
return b
340+
}
341+
```
314342
343+
> 动态规划:版本一
315344
```Go
316345
func maxProfit(prices []int) int {
317346
length:=len(prices)
@@ -338,6 +367,29 @@ func max(a,b int)int {
338367
}
339368
```
340369

370+
> 动态规划:版本二
371+
```Go
372+
func maxProfit(prices []int) int {
373+
dp := [2][2]int{}
374+
dp[0][0] = -prices[0]
375+
dp[0][1] = 0
376+
for i := 1; i < len(prices); i++{
377+
dp[i%2][0] = max(dp[(i-1)%2][0], -prices[i])
378+
dp[i%2][1] = max(dp[(i-1)%2][1], dp[(i-1)%2][0]+prices[i])
379+
}
380+
381+
return dp[(len(prices)-1)%2][1]
382+
}
383+
384+
func max(a, b int) int {
385+
if a > b{
386+
return a
387+
}
388+
389+
return b
390+
}
391+
```
392+
341393
JavaScript:
342394

343395
> 动态规划

problems/0122.买卖股票的最佳时机II.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,19 @@ const maxProfit = (prices) => {
264264
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
265265
}
266266

267-
return dp[prices.length -1][0];
267+
return dp[prices.length -1][1];
268+
};
269+
```
270+
271+
TypeScript:
272+
273+
```typescript
274+
function maxProfit(prices: number[]): number {
275+
let resProfit: number = 0;
276+
for (let i = 1, length = prices.length; i < length; i++) {
277+
resProfit += Math.max(prices[i] - prices[i - 1], 0);
278+
}
279+
return resProfit;
268280
};
269281
```
270282

problems/0122.买卖股票的最佳时机II(动态规划).md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ const maxProfit = (prices) => {
276276
dp[i][1] = Math.max(dp[i-1][1], dp[i-1][0] + prices[i]);
277277
}
278278

279-
return dp[prices.length -1][0];
279+
return dp[prices.length -1][1];
280280
};
281281

282282
// 方法二:动态规划(滚动数组)

problems/0134.加油站.md

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class Solution {
235235
return index;
236236
}
237237
}
238-
```
238+
```
239239

240240
### Python
241241
```python
@@ -364,7 +364,50 @@ var canCompleteCircuit = function(gas, cost) {
364364
};
365365
```
366366

367+
### TypeScript
368+
369+
**暴力法:**
370+
371+
```typescript
372+
function canCompleteCircuit(gas: number[], cost: number[]): number {
373+
for (let i = 0, length = gas.length; i < length; i++) {
374+
let curSum: number = 0;
375+
let index: number = i;
376+
while (curSum >= 0 && index < i + length) {
377+
let tempIndex: number = index % length;
378+
curSum += gas[tempIndex] - cost[tempIndex];
379+
index++;
380+
}
381+
if (index === i + length && curSum >= 0) return i;
382+
}
383+
return -1;
384+
};
385+
```
386+
387+
**解法二:**
388+
389+
```typescript
390+
function canCompleteCircuit(gas: number[], cost: number[]): number {
391+
let total: number = 0;
392+
let curGas: number = 0;
393+
let tempDiff: number = 0;
394+
let resIndex: number = 0;
395+
for (let i = 0, length = gas.length; i < length; i++) {
396+
tempDiff = gas[i] - cost[i];
397+
total += tempDiff;
398+
curGas += tempDiff;
399+
if (curGas < 0) {
400+
resIndex = i + 1;
401+
curGas = 0;
402+
}
403+
}
404+
if (total < 0) return -1;
405+
return resIndex;
406+
};
407+
```
408+
367409
### C
410+
368411
```c
369412
int canCompleteCircuit(int* gas, int gasSize, int* cost, int costSize){
370413
int curSum = 0;

problems/0135.分发糖果.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,32 @@ var candy = function(ratings) {
238238
};
239239
```
240240

241+
### TypeScript
242+
243+
```typescript
244+
function candy(ratings: number[]): number {
245+
const candies: number[] = [];
246+
candies[0] = 1;
247+
// 保证右边高分孩子一定比左边低分孩子发更多的糖果
248+
for (let i = 1, length = ratings.length; i < length; i++) {
249+
if (ratings[i] > ratings[i - 1]) {
250+
candies[i] = candies[i - 1] + 1;
251+
} else {
252+
candies[i] = 1;
253+
}
254+
}
255+
// 保证左边高分孩子一定比右边低分孩子发更多的糖果
256+
for (let i = ratings.length - 2; i >= 0; i--) {
257+
if (ratings[i] > ratings[i + 1]) {
258+
candies[i] = Math.max(candies[i], candies[i + 1] + 1);
259+
}
260+
}
261+
return candies.reduce((pre, cur) => pre + cur);
262+
};
263+
```
264+
265+
266+
241267

242268
-----------------------
243269
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

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

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

5151
## 01背包问题
5252

53-
背包问题,大家都知道,有N件物品和一个最多能被重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
53+
背包问题,大家都知道,有N件物品和一个最多能背重量为W 的背包。第i件物品的重量是weight[i],得到的价值是value[i] 。每件物品只能用一次,求解将哪些物品装入背包里物品价值总和最大。
5454

5555
**背包问题有多种背包方式,常见的有:01背包、完全背包、多重背包、分组背包和混合背包等等。**
5656

0 commit comments

Comments
 (0)