mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge pull request #1464 from wzqwtt/greedy02
添加(0053.最大子序和、0122.买卖股票的最佳时机II) Scala版本
This commit is contained in:
@ -333,8 +333,41 @@ function maxSubArray(nums: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
### Scala
|
||||
|
||||
**贪心**
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
def maxSubArray(nums: Array[Int]): Int = {
|
||||
var result = Int.MinValue
|
||||
var count = 0
|
||||
for (i <- nums.indices) {
|
||||
count += nums(i) // count累加
|
||||
if (count > result) result = count // 记录最大值
|
||||
if (count <= 0) count = 0 // 一旦count为负,则count归0
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
**动态规划**
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
def maxSubArray(nums: Array[Int]): Int = {
|
||||
var dp = new Array[Int](nums.length)
|
||||
var result = nums(0)
|
||||
dp(0) = nums(0)
|
||||
for (i <- 1 until nums.length) {
|
||||
dp(i) = math.max(nums(i), dp(i - 1) + nums(i))
|
||||
result = math.max(result, dp(i)) // 更新最大值
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -186,6 +186,24 @@ const maxSubArray = nums => {
|
||||
};
|
||||
```
|
||||
|
||||
|
||||
Scala:
|
||||
|
||||
```scala
|
||||
object Solution {
|
||||
def maxSubArray(nums: Array[Int]): Int = {
|
||||
var dp = new Array[Int](nums.length)
|
||||
var result = nums(0)
|
||||
dp(0) = nums(0)
|
||||
for (i <- 1 until nums.length) {
|
||||
dp(i) = math.max(nums(i), dp(i - 1) + nums(i))
|
||||
result = math.max(result, dp(i)) // 更新最大值
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
@ -205,6 +223,5 @@ function maxSubArray(nums: number[]): number {
|
||||
```
|
||||
|
||||
|
||||
|
||||
-----------------------
|
||||
<div align="center"><img src=https://code-thinking.cdn.bcebos.com/pics/01二维码一.jpg width=500> </img></div>
|
||||
|
@ -133,8 +133,9 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
Java:
|
||||
### Java:
|
||||
|
||||
贪心:
|
||||
```java
|
||||
// 贪心思路
|
||||
class Solution {
|
||||
@ -148,6 +149,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
动态规划:
|
||||
```java
|
||||
class Solution { // 动态规划
|
||||
public int maxProfit(int[] prices) {
|
||||
@ -169,8 +171,8 @@ class Solution { // 动态规划
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
|
||||
### Python:
|
||||
贪心:
|
||||
```python
|
||||
class Solution:
|
||||
def maxProfit(self, prices: List[int]) -> int:
|
||||
@ -180,7 +182,7 @@ class Solution:
|
||||
return result
|
||||
```
|
||||
|
||||
python动态规划
|
||||
动态规划:
|
||||
```python
|
||||
class Solution:
|
||||
def maxProfit(self, prices: List[int]) -> int:
|
||||
@ -194,7 +196,7 @@ class Solution:
|
||||
return dp[-1][1]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```golang
|
||||
//贪心算法
|
||||
@ -231,7 +233,7 @@ func maxProfit(prices []int) int {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript:
|
||||
### Javascript:
|
||||
|
||||
贪心
|
||||
```Javascript
|
||||
@ -268,7 +270,7 @@ const maxProfit = (prices) => {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function maxProfit(prices: number[]): number {
|
||||
@ -280,7 +282,7 @@ function maxProfit(prices: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
C:
|
||||
### C:
|
||||
贪心:
|
||||
```c
|
||||
int maxProfit(int* prices, int pricesSize){
|
||||
@ -318,5 +320,22 @@ int maxProfit(int* prices, int pricesSize){
|
||||
}
|
||||
```
|
||||
|
||||
### Scala
|
||||
|
||||
贪心:
|
||||
```scala
|
||||
object Solution {
|
||||
def maxProfit(prices: Array[Int]): Int = {
|
||||
var result = 0
|
||||
for (i <- 1 until prices.length) {
|
||||
if (prices(i) > prices(i - 1)) {
|
||||
result += prices(i) - prices(i - 1)
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
-----------------------
|
||||
<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