Skip to content

Commit 3992de9

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 752cfda + 60193de commit 3992de9

6 files changed

+177
-42
lines changed

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,46 @@ var maxProfit = function(prices) {
426426
};
427427
```
428428

429+
TypeScript:
430+
431+
> 贪心法
432+
433+
```typescript
434+
function maxProfit(prices: number[]): number {
435+
if (prices.length === 0) return 0;
436+
let buy: number = prices[0];
437+
let profitMax: number = 0;
438+
for (let i = 1, length = prices.length; i < length; i++) {
439+
profitMax = Math.max(profitMax, prices[i] - buy);
440+
buy = Math.min(prices[i], buy);
441+
}
442+
return profitMax;
443+
};
444+
```
445+
446+
> 动态规划
447+
448+
```typescript
449+
function maxProfit(prices: number[]): number {
450+
/**
451+
dp[i][0]: 第i天持有股票的最大现金
452+
dp[i][1]: 第i天不持有股票的最大现金
453+
*/
454+
const length = prices.length;
455+
if (length === 0) return 0;
456+
const dp: number[][] = [];
457+
dp[0] = [-prices[0], 0];
458+
for (let i = 1; i < length; i++) {
459+
dp[i] = [];
460+
dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
461+
dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
462+
}
463+
return dp[length - 1][1];
464+
};
465+
```
466+
467+
468+
429469

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

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,42 @@ const maxProfit = (prices) => {
295295
}
296296
```
297297

298+
TypeScript:
299+
300+
> 动态规划
301+
302+
```typescript
303+
function maxProfit(prices: number[]): number {
304+
/**
305+
dp[i][0]: 第i天持有股票
306+
dp[i][1]: 第i天不持有股票
307+
*/
308+
const length: number = prices.length;
309+
if (length === 0) return 0;
310+
const dp: number[][] = new Array(length).fill(0).map(_ => []);
311+
dp[0] = [-prices[0], 0];
312+
for (let i = 1; i < length; i++) {
313+
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
314+
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
315+
}
316+
return dp[length - 1][1];
317+
};
318+
```
319+
320+
> 贪心法
321+
322+
```typescript
323+
function maxProfit(prices: number[]): number {
324+
let resProfit: number = 0;
325+
for (let i = 1, length = prices.length; i < length; i++) {
326+
if (prices[i] > prices[i - 1]) {
327+
resProfit += prices[i] - prices[i - 1];
328+
}
329+
}
330+
return resProfit;
331+
};
332+
```
333+
298334

299335

300336
-----------------------

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,36 @@ const maxProfit = prices => {
352352
};
353353
```
354354

355+
TypeScript:
356+
357+
> 版本一
358+
359+
```typescript
360+
function maxProfit(prices: number[]): number {
361+
/**
362+
dp[i][0]: 无操作;
363+
dp[i][1]: 第一次买入;
364+
dp[i][2]: 第一次卖出;
365+
dp[i][3]: 第二次买入;
366+
dp[i][4]: 第二次卖出;
367+
*/
368+
const length: number = prices.length;
369+
if (length === 0) return 0;
370+
const dp: number[][] = new Array(length).fill(0)
371+
.map(_ => new Array(5).fill(0));
372+
dp[0][1] = -prices[0];
373+
dp[0][3] = -prices[0];
374+
for (let i = 1; i < length; i++) {
375+
dp[i][0] = dp[i - 1][0];
376+
dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
377+
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
378+
dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
379+
dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
380+
}
381+
return Math.max(dp[length - 1][2], dp[length - 1][4]);
382+
};
383+
```
384+
355385
Go:
356386

357387
> 版本一:

problems/0142.环形链表II.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,31 @@ ListNode *detectCycle(ListNode *head) {
370370
}
371371
```
372372
373-
373+
Scala:
374+
```scala
375+
object Solution {
376+
def detectCycle(head: ListNode): ListNode = {
377+
var fast = head // 快指针
378+
var slow = head // 慢指针
379+
while (fast != null && fast.next != null) {
380+
fast = fast.next.next // 快指针一次走两步
381+
slow = slow.next // 慢指针一次走一步
382+
// 如果相遇,fast快指针回到头
383+
if (fast == slow) {
384+
fast = head
385+
// 两个指针一步一步的走,第一次相遇的节点必是入环节点
386+
while (fast != slow) {
387+
fast = fast.next
388+
slow = slow.next
389+
}
390+
return fast
391+
}
392+
}
393+
// 如果fast指向空值,必然无环返回null
394+
null
395+
}
396+
}
397+
```
374398

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

problems/0242.有效的字母异位词.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,32 @@ impl Solution {
308308
}
309309
```
310310

311+
312+
Scala:
313+
```scala
314+
object Solution {
315+
def isAnagram(s: String, t: String): Boolean = {
316+
// 如果两个字符串的长度不等,直接返回false
317+
if (s.length != t.length) return false
318+
val record = new Array[Int](26) // 记录每个单词出现了多少次
319+
// 遍历字符串,对于s字符串单词对应的记录+=1,t字符串对应的记录-=1
320+
for (i <- 0 until s.length) {
321+
record(s(i) - 97) += 1
322+
record(t(i) - 97) -= 1
323+
}
324+
// 如果不等于则直接返回false
325+
for (i <- 0 until 26) {
326+
if (record(i) != 0) {
327+
return false
328+
}
329+
}
330+
// 如果前面不返回false,说明匹配成功,返回true,return可以省略
331+
true
332+
}
333+
}
334+
```
335+
336+
311337
C#:
312338
```csharp
313339
public bool IsAnagram(string s, string t) {
@@ -326,6 +352,7 @@ C#:
326352
return true;
327353
}
328354
```
355+
329356
## 相关题目
330357

331358
* 383.赎金信

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

Lines changed: 19 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -380,53 +380,31 @@ func main() {
380380
### javascript
381381

382382
```js
383-
/**
384-
*
385-
* @param {Number []} weight
386-
* @param {Number []} value
387-
* @param {Number} size
388-
* @returns
389-
*/
390-
391-
function testWeightBagProblem(weight, value, size) {
392-
const len = weight.length,
393-
dp = Array.from({length: len}).map(
394-
() => Array(size + 1)) //JavaScript 数组是引用类型
395-
for(let i = 0; i < len; i++) { //初始化最左一列,即背包容量为0时的情况
396-
dp[i][0] = 0;
397-
}
398-
for(let j = 1; j < size+1; j++) { //初始化第0行, 只有一件物品的情况
399-
if(weight[0] <= j) {
400-
dp[0][j] = value[0];
401-
} else {
402-
dp[0][j] = 0;
403-
}
404-
}
405-
406-
for(let i = 1; i < len; i++) { //dp[i][j]由其左上方元素推导得出
407-
for(let j = 1; j < size+1; j++) {
408-
if(j < weight[i]) dp[i][j] = dp[i - 1][j];
409-
else dp[i][j] = Math.max(dp[i-1][j], dp[i-1][j - weight[i]] + value[i]);
410-
}
411-
}
383+
function testWeightBagProblem (weight, value, size) {
384+
// 定义 dp 数组
385+
const len = weight.length,
386+
dp = Array(len).fill().map(() => Array(size + 1).fill(0));
412387

413-
return dp[len-1][size] //满足条件的最大值
414-
}
388+
// 初始化
389+
for(let j = weight[0]; j <= size; j++) {
390+
dp[0][j] = value[0];
391+
}
415392

416-
function testWeightBagProblem2 (wight, value, size) {
417-
const len = wight.length,
418-
dp = Array(size + 1).fill(0);
419-
for(let i = 1; i <= len; i++) {
420-
for(let j = size; j >= wight[i - 1]; j--) {
421-
dp[j] = Math.max(dp[j], value[i - 1] + dp[j - wight[i - 1]]);
393+
// weight 数组的长度len 就是物品个数
394+
for(let i = 1; i < len; i++) { // 遍历物品
395+
for(let j = 0; j <= size; j++) { // 遍历背包容量
396+
if(j < weight[i]) dp[i][j] = dp[i - 1][j];
397+
else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
398+
}
422399
}
423-
}
424-
return dp[size];
425-
}
426400

401+
console.table(dp)
402+
403+
return dp[len - 1][size];
404+
}
427405

428406
function test () {
429-
console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6));
407+
console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6));
430408
}
431409

432410
test();

0 commit comments

Comments
 (0)