mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge pull request #1483 from wzqwtt/greedy08
添加(0714.买卖股票的最佳时机含手续费、0968.监控二叉树) Scala版本
This commit is contained in:
@ -153,7 +153,7 @@ public:
|
|||||||
|
|
||||||
## 其他语言版本
|
## 其他语言版本
|
||||||
|
|
||||||
Java:
|
### Java
|
||||||
```java
|
```java
|
||||||
// 贪心思路
|
// 贪心思路
|
||||||
class Solution {
|
class Solution {
|
||||||
@ -198,7 +198,7 @@ class Solution { // 动态规划
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
Python:
|
### Python
|
||||||
|
|
||||||
```python
|
```python
|
||||||
class Solution: # 贪心思路
|
class Solution: # 贪心思路
|
||||||
@ -216,7 +216,7 @@ class Solution: # 贪心思路
|
|||||||
return result
|
return result
|
||||||
```
|
```
|
||||||
|
|
||||||
Go:
|
### Go
|
||||||
```golang
|
```golang
|
||||||
func maxProfit(prices []int, fee int) int {
|
func maxProfit(prices []int, fee int) int {
|
||||||
var minBuy int = prices[0] //第一天买入
|
var minBuy int = prices[0] //第一天买入
|
||||||
@ -241,7 +241,7 @@ func maxProfit(prices []int, fee int) int {
|
|||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
Javascript:
|
### Javascript
|
||||||
```Javascript
|
```Javascript
|
||||||
// 贪心思路
|
// 贪心思路
|
||||||
var maxProfit = function(prices, fee) {
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||||
|
Reference in New Issue
Block a user