diff --git a/problems/0121.买卖股票的最佳时机.md b/problems/0121.买卖股票的最佳时机.md index f0bc3b97..a2498bb6 100644 --- a/problems/0121.买卖股票的最佳时机.md +++ b/problems/0121.买卖股票的最佳时机.md @@ -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]; +}; +``` + + + -----------------------
diff --git a/problems/0122.买卖股票的最佳时机II(动态规划).md b/problems/0122.买卖股票的最佳时机II(动态规划).md index 5a165a14..12b21fde 100644 --- a/problems/0122.买卖股票的最佳时机II(动态规划).md +++ b/problems/0122.买卖股票的最佳时机II(动态规划).md @@ -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; +}; +``` + ----------------------- diff --git a/problems/0123.买卖股票的最佳时机III.md b/problems/0123.买卖股票的最佳时机III.md index 56ade343..67c99497 100644 --- a/problems/0123.买卖股票的最佳时机III.md +++ b/problems/0123.买卖股票的最佳时机III.md @@ -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: > 版本一: diff --git a/problems/0142.环形链表II.md b/problems/0142.环形链表II.md index e8ca950d..f8e62d45 100644 --- a/problems/0142.环形链表II.md +++ b/problems/0142.环形链表II.md @@ -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 + } +} +``` -----------------------
diff --git a/problems/0242.有效的字母异位词.md b/problems/0242.有效的字母异位词.md index 878b2466..8fd9c604 100644 --- a/problems/0242.有效的字母异位词.md +++ b/problems/0242.有效的字母异位词.md @@ -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字符串单词对应的记录+=1,t字符串对应的记录-=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,说明匹配成功,返回true,return可以省略 + true + } +} +``` + + C#: ```csharp public bool IsAnagram(string s, string t) { @@ -326,6 +352,7 @@ C#: return true; } ``` + ## 相关题目 * 383.赎金信 diff --git a/problems/背包理论基础01背包-1.md b/problems/背包理论基础01背包-1.md index d2ab191a..a40a92ab 100644 --- a/problems/背包理论基础01背包-1.md +++ b/problems/背包理论基础01背包-1.md @@ -380,53 +380,31 @@ 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(len).fill().map(() => Array(size + 1).fill(0)); -function testWeightBagProblem(weight, value, size) { -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[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++) { -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] //满足条件的最大值 -} - -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]]); + // 初始化 + for(let j = weight[0]; j <= size; j++) { + dp[0][j] = value[0]; } - } - return dp[size]; -} + // 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]); + } + } + + console.table(dp) + + return dp[len - 1][size]; +} function test () { - console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6)); + console.log(testWeightBagProblem([1, 3, 4, 5], [15, 20, 30, 55], 6)); } test();