Merge branch 'youngyangyang04:master' into master

This commit is contained in:
桜小路七葉
2022-06-01 16:49:34 +08:00
committed by GitHub
6 changed files with 178 additions and 43 deletions

View File

@ -426,6 +426,46 @@ var maxProfit = function(prices) {
};
```
TypeScript
> 贪心法
```typescript
function maxProfit(prices: number[]): number {
if (prices.length === 0) return 0;
let buy: number = prices[0];
let profitMax: number = 0;
for (let i = 1, length = prices.length; i < length; i++) {
profitMax = Math.max(profitMax, prices[i] - buy);
buy = Math.min(prices[i], buy);
}
return profitMax;
};
```
> 动态规划
```typescript
function maxProfit(prices: number[]): number {
/**
dp[i][0]: 第i天持有股票的最大现金
dp[i][1]: 第i天不持有股票的最大现金
*/
const length = prices.length;
if (length === 0) return 0;
const dp: number[][] = [];
dp[0] = [-prices[0], 0];
for (let i = 1; i < length; i++) {
dp[i] = [];
dp[i][0] = Math.max(dp[i - 1][0], -prices[i]);
dp[i][1] = Math.max(dp[i - 1][0] + prices[i], dp[i - 1][1]);
}
return dp[length - 1][1];
};
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -295,6 +295,42 @@ const maxProfit = (prices) => {
}
```
TypeScript
> 动态规划
```typescript
function maxProfit(prices: number[]): number {
/**
dp[i][0]: 第i天持有股票
dp[i][1]: 第i天不持有股票
*/
const length: number = prices.length;
if (length === 0) return 0;
const dp: number[][] = new Array(length).fill(0).map(_ => []);
dp[0] = [-prices[0], 0];
for (let i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] + prices[i]);
}
return dp[length - 1][1];
};
```
> 贪心法
```typescript
function maxProfit(prices: number[]): number {
let resProfit: number = 0;
for (let i = 1, length = prices.length; i < length; i++) {
if (prices[i] > prices[i - 1]) {
resProfit += prices[i] - prices[i - 1];
}
}
return resProfit;
};
```
-----------------------

View File

@ -352,6 +352,36 @@ const maxProfit = prices => {
};
```
TypeScript
> 版本一
```typescript
function maxProfit(prices: number[]): number {
/**
dp[i][0]: 无操作;
dp[i][1]: 第一次买入;
dp[i][2]: 第一次卖出;
dp[i][3]: 第二次买入;
dp[i][4]: 第二次卖出;
*/
const length: number = prices.length;
if (length === 0) return 0;
const dp: number[][] = new Array(length).fill(0)
.map(_ => new Array(5).fill(0));
dp[0][1] = -prices[0];
dp[0][3] = -prices[0];
for (let i = 1; i < length; i++) {
dp[i][0] = dp[i - 1][0];
dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1] + prices[i]);
dp[i][3] = Math.max(dp[i - 1][3], dp[i - 1][2] - prices[i]);
dp[i][4] = Math.max(dp[i - 1][4], dp[i - 1][3] + prices[i]);
}
return Math.max(dp[length - 1][2], dp[length - 1][4]);
};
```
Go:
> 版本一:

View File

@ -370,7 +370,31 @@ ListNode *detectCycle(ListNode *head) {
}
```
Scala:
```scala
object Solution {
def detectCycle(head: ListNode): ListNode = {
var fast = head // 快指针
var slow = head // 慢指针
while (fast != null && fast.next != null) {
fast = fast.next.next // 快指针一次走两步
slow = slow.next // 慢指针一次走一步
// 如果相遇fast快指针回到头
if (fast == slow) {
fast = head
// 两个指针一步一步的走,第一次相遇的节点必是入环节点
while (fast != slow) {
fast = fast.next
slow = slow.next
}
return fast
}
}
// 如果fast指向空值必然无环返回null
null
}
}
```
-----------------------
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>

View File

@ -308,6 +308,32 @@ impl Solution {
}
```
Scala:
```scala
object Solution {
def isAnagram(s: String, t: String): Boolean = {
// 如果两个字符串的长度不等直接返回false
if (s.length != t.length) return false
val record = new Array[Int](26) // 记录每个单词出现了多少次
// 遍历字符串对于s字符串单词对应的记录+=1t字符串对应的记录-=1
for (i <- 0 until s.length) {
record(s(i) - 97) += 1
record(t(i) - 97) -= 1
}
// 如果不等于则直接返回false
for (i <- 0 until 26) {
if (record(i) != 0) {
return false
}
}
// 如果前面不返回false说明匹配成功返回truereturn可以省略
true
}
}
```
C#
```csharp
public bool IsAnagram(string s, string t) {
@ -326,6 +352,7 @@ C#
return true;
}
```
## 相关题目
* 383.赎金信

View File

@ -380,50 +380,28 @@ func main() {
### javascript
```js
/**
*
* @param {Number []} weight
* @param {Number []} value
* @param {Number} size
* @returns
*/
function testWeightBagProblem (weight, value, size) {
// 定义 dp 数组
const len = weight.length,
dp = Array.from({length: len}).map(
() => Array(size + 1)) //JavaScript 数组是引用类型
for(let i = 0; i < len; i++) { //初始化最左一列即背包容量为0时的情况
dp[i][0] = 0;
}
for(let j = 1; j < size+1; j++) { //初始化第0行, 只有一件物品的情况
if(weight[0] <= j) {
dp = Array(len).fill().map(() => Array(size + 1).fill(0));
// 初始化
for(let j = weight[0]; j <= size; j++) {
dp[0][j] = value[0];
} else {
dp[0][j] = 0;
}
}
for(let i = 1; i < len; i++) { //dp[i][j]由其左上方元素推导得出
for(let j = 1; j < size+1; j++) {
// weight 数组的长度len 就是物品个数
for(let i = 1; i < len; i++) { // 遍历物品
for(let j = 0; j <= size; j++) { // 遍历背包容量
if(j < weight[i]) dp[i][j] = dp[i - 1][j];
else dp[i][j] = Math.max(dp[i - 1][j], dp[i - 1][j - weight[i]] + value[i]);
}
}
return dp[len-1][size] //满足条件的最大值
}
console.table(dp)
function testWeightBagProblem2 (wight, value, size) {
const len = wight.length,
dp = Array(size + 1).fill(0);
for(let i = 1; i <= len; i++) {
for(let j = size; j >= wight[i - 1]; j--) {
dp[j] = Math.max(dp[j], value[i - 1] + dp[j - wight[i - 1]]);
return dp[len - 1][size];
}
}
return dp[size];
}
function test () {
console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6));