From a031937e874267a3a4dcbaf42eba272b244fc0fc Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Thu, 16 Jun 2022 22:32:13 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200714.=E4=B9=B0?= =?UTF-8?q?=E5=8D=96=E8=82=A1=E7=A5=A8=E7=9A=84=E6=9C=80=E4=BD=B3=E6=97=B6?= =?UTF-8?q?=E6=9C=BA=E5=90=AB=E6=89=8B=E7=BB=AD=E8=B4=B9.md=20Scala?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...买卖股票的最佳时机含手续费.md | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/problems/0714.买卖股票的最佳时机含手续费.md b/problems/0714.买卖股票的最佳时机含手续费.md index b27631c6..4bc21a70 100644 --- a/problems/0714.买卖股票的最佳时机含手续费.md +++ b/problems/0714.买卖股票的最佳时机含手续费.md @@ -153,7 +153,7 @@ public: ## 其他语言版本 -Java: +### Java ```java // 贪心思路 class Solution { @@ -198,7 +198,7 @@ class Solution { // 动态规划 -Python: +### Python ```python class Solution: # 贪心思路 @@ -216,7 +216,7 @@ class Solution: # 贪心思路 return result ``` -Go: +### Go ```golang func maxProfit(prices []int, fee int) int { var minBuy int = prices[0] //第一天买入 @@ -241,7 +241,7 @@ func maxProfit(prices []int, fee int) int { return res } ``` -Javascript: +### Javascript ```Javascript // 贪心思路 var maxProfit = function(prices, fee) { @@ -293,7 +293,7 @@ var maxProfit = function(prices, fee) { }; ``` -TypeScript: +### TypeScript > 贪心 @@ -335,8 +335,28 @@ function maxProfit(prices: number[], fee: number): number { }; ``` +### Scala +贪心思路: +```scala +object Solution { + def maxProfit(prices: Array[Int], fee: Int): Int = { + var result = 0 + var minPrice = prices(0) + for (i <- 1 until prices.length) { + if (prices(i) < minPrice) { + minPrice = prices(i) // 比当前最小值还小 + } + if (prices(i) > minPrice + fee) { + result += prices(i) - minPrice - fee + minPrice = prices(i) - fee + } + } + result + } +} +``` -----------------------
From da559f0ef013bb0503919bda084b244975a73ed6 Mon Sep 17 00:00:00 2001 From: ZongqinWang <1722249371@qq.com> Date: Sat, 18 Jun 2022 19:17:49 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200968.=E7=9B=91?= =?UTF-8?q?=E6=8E=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20Scala=E7=89=88?= =?UTF-8?q?=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0968.监控二叉树.md | 35 ++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/problems/0968.监控二叉树.md b/problems/0968.监控二叉树.md index 9a510a1b..6c957eb2 100644 --- a/problems/0968.监控二叉树.md +++ b/problems/0968.监控二叉树.md @@ -544,5 +544,40 @@ int minCameraCover(struct TreeNode* root){ } ``` +### Scala + +```scala +object Solution { + def minCameraCover(root: TreeNode): Int = { + var result = 0 + def traversal(cur: TreeNode): Int = { + // 空节点,该节点有覆盖 + if (cur == null) return 2 + var left = traversal(cur.left) + var right = traversal(cur.right) + // 情况1,左右节点都有覆盖 + if (left == 2 && right == 2) { + return 0 + } + // 情况2 + if (left == 0 || right == 0) { + result += 1 + return 1 + } + // 情况3 + if (left == 1 || right == 1) { + return 2 + } + -1 + } + + if (traversal(root) == 0) { + result += 1 + } + result + } +} +``` + -----------------------