mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 23:28:29 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -240,6 +240,42 @@ class Solution:
|
|||||||
res = max(res, dp[i])
|
res = max(res, dp[i])
|
||||||
return res
|
return res
|
||||||
```
|
```
|
||||||
|
|
||||||
|
动态规划
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def maxSubArray(self, nums):
|
||||||
|
if not nums:
|
||||||
|
return 0
|
||||||
|
dp = [0] * len(nums) # dp[i]表示包括i之前的最大连续子序列和
|
||||||
|
dp[0] = nums[0]
|
||||||
|
result = dp[0]
|
||||||
|
for i in range(1, len(nums)):
|
||||||
|
dp[i] = max(dp[i-1]+nums[i], nums[i]) # 状态转移公式
|
||||||
|
if dp[i] > result:
|
||||||
|
result = dp[i] # result 保存dp[i]的最大值
|
||||||
|
return result
|
||||||
|
```
|
||||||
|
|
||||||
|
动态规划优化
|
||||||
|
|
||||||
|
```python
|
||||||
|
class Solution:
|
||||||
|
def maxSubArray(self, nums: List[int]) -> int:
|
||||||
|
max_sum = float("-inf") # 初始化结果为负无穷大,方便比较取最大值
|
||||||
|
current_sum = 0 # 初始化当前连续和
|
||||||
|
|
||||||
|
for num in nums:
|
||||||
|
|
||||||
|
# 更新当前连续和
|
||||||
|
# 如果原本的连续和加上当前数字之后没有当前数字大,说明原本的连续和是负数,那么就直接从当前数字开始重新计算连续和
|
||||||
|
current_sum = max(current_sum+num, num)
|
||||||
|
max_sum = max(max_sum, current_sum) # 更新结果
|
||||||
|
|
||||||
|
return max_sum
|
||||||
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
贪心法
|
贪心法
|
||||||
```go
|
```go
|
||||||
|
@ -143,6 +143,23 @@ class Solution:
|
|||||||
return False
|
return False
|
||||||
```
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
## 基于当前最远可到达位置判断
|
||||||
|
class Solution:
|
||||||
|
def canJump(self, nums: List[int]) -> bool:
|
||||||
|
far = nums[0]
|
||||||
|
for i in range(len(nums)):
|
||||||
|
# 要考虑两个情况
|
||||||
|
# 1. i <= far - 表示 当前位置i 可以到达
|
||||||
|
# 2. i > far - 表示 当前位置i 无法到达
|
||||||
|
if i > far:
|
||||||
|
return False
|
||||||
|
far = max(far, nums[i]+i)
|
||||||
|
# 如果循环正常结束,表示最后一个位置也可以到达,否则会在中途直接退出
|
||||||
|
# 关键点在于,要想明白其实列表中的每个位置都是需要验证能否到达的
|
||||||
|
return True
|
||||||
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
@ -177,21 +177,20 @@ class Solution {
|
|||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
def candy(self, ratings: List[int]) -> int:
|
def candy(self, ratings: List[int]) -> int:
|
||||||
candyVec = [1] * len(ratings)
|
n = len(ratings)
|
||||||
|
candies = [1] * n
|
||||||
|
|
||||||
# 从前向后遍历,处理右侧比左侧评分高的情况
|
# Forward pass: handle cases where right rating is higher than left
|
||||||
for i in range(1, len(ratings)):
|
for i in range(1, n):
|
||||||
if ratings[i] > ratings[i - 1]:
|
if ratings[i] > ratings[i - 1]:
|
||||||
candyVec[i] = candyVec[i - 1] + 1
|
candies[i] = candies[i - 1] + 1
|
||||||
|
|
||||||
# 从后向前遍历,处理左侧比右侧评分高的情况
|
# Backward pass: handle cases where left rating is higher than right
|
||||||
for i in range(len(ratings) - 2, -1, -1):
|
for i in range(n - 2, -1, -1):
|
||||||
if ratings[i] > ratings[i + 1]:
|
if ratings[i] > ratings[i + 1]:
|
||||||
candyVec[i] = max(candyVec[i], candyVec[i + 1] + 1)
|
candies[i] = max(candies[i], candies[i + 1] + 1)
|
||||||
|
|
||||||
# 统计结果
|
return sum(candies)
|
||||||
result = sum(candyVec)
|
|
||||||
return result
|
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -108,7 +108,7 @@ public:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int result = st.top();
|
long long result = st.top();
|
||||||
st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
|
st.pop(); // 把栈里最后一个元素弹出(其实不弹出也没事)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
@ -164,7 +164,7 @@ class Solution {
|
|||||||
int top = -1;
|
int top = -1;
|
||||||
for (int i = 0; i < s.length(); i++) {
|
for (int i = 0; i < s.length(); i++) {
|
||||||
char c = s.charAt(i);
|
char c = s.charAt(i);
|
||||||
// 当 top > 0,即栈中有字符时,当前字符如果和栈中字符相等,弹出栈顶字符,同时 top--
|
// 当 top >= 0,即栈中有字符时,当前字符如果和栈中字符相等,弹出栈顶字符,同时 top--
|
||||||
if (top >= 0 && res.charAt(top) == c) {
|
if (top >= 0 && res.charAt(top) == c) {
|
||||||
res.deleteCharAt(top);
|
res.deleteCharAt(top);
|
||||||
top--;
|
top--;
|
||||||
|
@ -412,6 +412,46 @@ const dfs = (graph, visited, x, y) => {
|
|||||||
### Swift
|
### Swift
|
||||||
|
|
||||||
### Scala
|
### Scala
|
||||||
|
```scala
|
||||||
|
import util.control.Breaks._
|
||||||
|
|
||||||
|
object Solution {
|
||||||
|
val dir = List((-1,0), (0,-1), (1,0), (0,1)) // 四个方向
|
||||||
|
|
||||||
|
def dfs(grid: Array[Array[Char]], visited: Array[Array[Boolean]], row: Int, col: Int): Unit = {
|
||||||
|
(0 until 4).map { x =>
|
||||||
|
val nextR = row + dir(x)(0)
|
||||||
|
val nextC = col + dir(x)(1)
|
||||||
|
breakable {
|
||||||
|
if(nextR < 0 || nextR >= grid.length || nextC < 0 || nextC >= grid(0).length) break
|
||||||
|
if (!visited(nextR)(nextC) && grid(nextR)(nextC) == '1') {
|
||||||
|
visited(nextR)(nextC) = true // 经过就记录
|
||||||
|
dfs(grid, visited, nextR, nextC)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
def numIslands(grid: Array[Array[Char]]): Int = {
|
||||||
|
val row = grid.length
|
||||||
|
val col = grid(0).length
|
||||||
|
var visited = Array.fill(row)(Array.fill(col)(false))
|
||||||
|
var counter = 0
|
||||||
|
|
||||||
|
(0 until row).map{ r =>
|
||||||
|
(0 until col).map{ c =>
|
||||||
|
if (!visited(r)(c) && grid(r)(c) == '1') {
|
||||||
|
visited(r)(c) = true // 经过就记录
|
||||||
|
dfs(grid, visited, r, c)
|
||||||
|
counter += 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
counter
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### C#
|
### C#
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user