mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -359,6 +359,26 @@ func max(a, b int) int {
|
|||||||
|
|
||||||
### Javascript:
|
### Javascript:
|
||||||
|
|
||||||
|
> 不同的状态定义 感觉更容易理解些
|
||||||
|
```javascript
|
||||||
|
function maxProfit(prices) {
|
||||||
|
// 第i天状态 持股 卖出 非冷冻期(不持股) 处于冷冻期
|
||||||
|
const dp = new Array(prices.length).fill(0).map(() => [0, 0, 0, 0]);
|
||||||
|
dp[0][0] = -prices[0];
|
||||||
|
for (let i = 1; i < prices.length; i++) {
|
||||||
|
// 持股
|
||||||
|
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][2] - prices[i]);
|
||||||
|
// 卖出
|
||||||
|
dp[i][1] = dp[i - 1][0] + prices[i];
|
||||||
|
// 非冷冻期(不持股)
|
||||||
|
dp[i][2] = Math.max(dp[i - 1][2], dp[i - 1][1]);
|
||||||
|
// 冷冻期(上一天卖出)
|
||||||
|
dp[i][3] = dp[i - 1][1];
|
||||||
|
}
|
||||||
|
return Math.max(...dp.pop());
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
```javascript
|
```javascript
|
||||||
const maxProfit = (prices) => {
|
const maxProfit = (prices) => {
|
||||||
if(prices.length < 2) {
|
if(prices.length < 2) {
|
||||||
|
@ -190,10 +190,164 @@ int main() {
|
|||||||
|
|
||||||
### Java
|
### Java
|
||||||
|
|
||||||
|
```java
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
static int[][] dir = { {0, 1}, {1, 0}, {-1, 0}, {0, -1} }; // 四个方向
|
||||||
|
|
||||||
|
public static void dfs(int[][] grid, boolean[][] visited, int x, int y) {
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
int nextx = x + dir[i][0];
|
||||||
|
int nexty = y + dir[i][1];
|
||||||
|
if (nextx < 0 || nextx >= grid.length || nexty < 0 || nexty >= grid[0].length) continue; // 越界了,直接跳过
|
||||||
|
if (!visited[nextx][nexty] && grid[nextx][nexty] == 1) { // 没有访问过的 同时 是陆地的
|
||||||
|
visited[nextx][nexty] = true;
|
||||||
|
dfs(grid, visited, nextx, nexty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
int n = scanner.nextInt();
|
||||||
|
int m = scanner.nextInt();
|
||||||
|
int[][] grid = new int[n][m];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
grid[i][j] = scanner.nextInt();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean[][] visited = new boolean[n][m];
|
||||||
|
|
||||||
|
int result = 0;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
for (int j = 0; j < m; j++) {
|
||||||
|
if (!visited[i][j] && grid[i][j] == 1) {
|
||||||
|
visited[i][j] = true;
|
||||||
|
result++; // 遇到没访问过的陆地,+1
|
||||||
|
dfs(grid, visited, i, j); // 将与其链接的陆地都标记上 true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println(result);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
|
```python
|
||||||
|
|
||||||
|
def dfs(grid, visited, x, y):
|
||||||
|
dir = [(0, 1), (1, 0), (-1, 0), (0, -1)] # 四个方向
|
||||||
|
for d in dir:
|
||||||
|
nextx, nexty = x + d[0], y + d[1]
|
||||||
|
if 0 <= nextx < len(grid) and 0 <= nexty < len(grid[0]):
|
||||||
|
if not visited[nextx][nexty] and grid[nextx][nexty] == 1: # 没有访问过的 同时 是陆地的
|
||||||
|
visited[nextx][nexty] = True
|
||||||
|
dfs(grid, visited, nextx, nexty)
|
||||||
|
|
||||||
|
def main():
|
||||||
|
n, m = map(int, input().split())
|
||||||
|
grid = [list(map(int, input().split())) for _ in range(n)]
|
||||||
|
visited = [[False] * m for _ in range(n)]
|
||||||
|
|
||||||
|
result = 0
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(m):
|
||||||
|
if not visited[i][j] and grid[i][j] == 1:
|
||||||
|
visited[i][j] = True
|
||||||
|
result += 1 # 遇到没访问过的陆地,+1
|
||||||
|
dfs(grid, visited, i, j) # 将与其链接的陆地都标记上 True
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
|
```go
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bufio"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dir = [4][2]int{{0, 1}, {1, 0}, {-1, 0}, {0, -1}} // 四个方向
|
||||||
|
|
||||||
|
func dfs(grid [][]int, visited [][]bool, x, y int) {
|
||||||
|
for i := 0; i < 4; i++ {
|
||||||
|
nextx := x + dir[i][0]
|
||||||
|
nexty := y + dir[i][1]
|
||||||
|
if nextx < 0 || nextx >= len(grid) || nexty < 0 || nexty >= len(grid[0]) {
|
||||||
|
continue // 越界了,直接跳过
|
||||||
|
}
|
||||||
|
if !visited[nextx][nexty] && grid[nextx][nexty] == 1 { // 没有访问过的 同时 是陆地的
|
||||||
|
visited[nextx][nexty] = true
|
||||||
|
dfs(grid, visited, nextx, nexty)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
var n, m int
|
||||||
|
fmt.Scanf("%d %d", &n, &m)
|
||||||
|
|
||||||
|
grid := make([][]int, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
grid[i] = make([]int, m)
|
||||||
|
line, _ := reader.ReadString('\n')
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
elements := strings.Split(line, " ")
|
||||||
|
for j := 0; j < m; j++ {
|
||||||
|
grid[i][j], _ = strconv.Atoi(elements[j])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
visited := make([][]bool, n)
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
visited[i] = make([]bool, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
result := 0
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
for j := 0; j < m; j++ {
|
||||||
|
if !visited[i][j] && grid[i][j] == 1 {
|
||||||
|
visited[i][j] = true
|
||||||
|
result++ // 遇到没访问过的陆地,+1
|
||||||
|
dfs(grid, visited, i, j) // 将与其链接的陆地都标记上 true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
@ -283,12 +437,62 @@ const bfs = (graph, visited, x, y) => {
|
|||||||
|
|
||||||
### PhP
|
### PhP
|
||||||
|
|
||||||
|
```PHP
|
||||||
|
|
||||||
|
<?php
|
||||||
|
|
||||||
|
function dfs($grid, &$visited, $x, $y) {
|
||||||
|
$dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向
|
||||||
|
foreach ($dir as $d) {
|
||||||
|
$nextx = $x + $d[0];
|
||||||
|
$nexty = $y + $d[1];
|
||||||
|
if ($nextx < 0 || $nextx >= count($grid) || $nexty < 0 || $nexty >= count($grid[0])) {
|
||||||
|
continue; // 越界了,直接跳过
|
||||||
|
}
|
||||||
|
if (!$visited[$nextx][$nexty] && $grid[$nextx][$nexty] == 1) { // 没有访问过的 同时 是陆地的
|
||||||
|
$visited[$nextx][$nexty] = true;
|
||||||
|
dfs($grid, $visited, $nextx, $nexty);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function main() {
|
||||||
|
fscanf(STDIN, "%d %d", $n, $m);
|
||||||
|
$grid = [];
|
||||||
|
for ($i = 0; $i < $n; $i++) {
|
||||||
|
$grid[$i] = array_map('intval', explode(' ', trim(fgets(STDIN))));
|
||||||
|
}
|
||||||
|
|
||||||
|
$visited = array_fill(0, $n, array_fill(0, $m, false));
|
||||||
|
|
||||||
|
$result = 0;
|
||||||
|
for ($i = 0; $i < $n; $i++) {
|
||||||
|
for ($j = 0; $j < $m; $j++) {
|
||||||
|
if (!$visited[$i][$j] && $grid[$i][$j] == 1) {
|
||||||
|
$visited[$i][$j] = true;
|
||||||
|
$result++; // 遇到没访问过的陆地,+1
|
||||||
|
dfs($grid, $visited, $i, $j); // 将与其链接的陆地都标记上 true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
echo $result . PHP_EOL;
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
|
?>
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Swift
|
### Swift
|
||||||
|
|
||||||
### Scala
|
### Scala
|
||||||
|
|
||||||
### C#
|
### C#
|
||||||
|
|
||||||
|
|
||||||
### Dart
|
### Dart
|
||||||
|
|
||||||
### C
|
### C
|
||||||
|
@ -226,6 +226,100 @@ public:
|
|||||||
|
|
||||||
### Python
|
### Python
|
||||||
|
|
||||||
|
DFS
|
||||||
|
|
||||||
|
```python
|
||||||
|
# 四个方向
|
||||||
|
position = [[0, 1], [1, 0], [0, -1], [-1, 0]]
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
|
||||||
|
def dfs(grid, visited, x, y):
|
||||||
|
"""
|
||||||
|
深度优先搜索,对一整块陆地进行标记
|
||||||
|
"""
|
||||||
|
global count # 定义全局变量,便于传递count值
|
||||||
|
for i, j in position:
|
||||||
|
cur_x = x + i
|
||||||
|
cur_y = y + j
|
||||||
|
# 下标越界,跳过
|
||||||
|
if cur_x < 0 or cur_x >= len(grid) or cur_y < 0 or cur_y >= len(grid[0]):
|
||||||
|
continue
|
||||||
|
if not visited[cur_x][cur_y] and grid[cur_x][cur_y] == 1:
|
||||||
|
visited[cur_x][cur_y] = True
|
||||||
|
count += 1
|
||||||
|
dfs(grid, visited, cur_x, cur_y)
|
||||||
|
|
||||||
|
|
||||||
|
n, m = map(int, input().split())
|
||||||
|
# 邻接矩阵
|
||||||
|
grid = []
|
||||||
|
for i in range(n):
|
||||||
|
grid.append(list(map(int, input().split())))
|
||||||
|
# 访问表
|
||||||
|
visited = [[False] * m for _ in range(n)]
|
||||||
|
|
||||||
|
result = 0 # 记录最终结果
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(m):
|
||||||
|
if grid[i][j] == 1 and not visited[i][j]:
|
||||||
|
count = 1
|
||||||
|
visited[i][j] = True
|
||||||
|
dfs(grid, visited, i, j)
|
||||||
|
result = max(count, result)
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
```
|
||||||
|
|
||||||
|
BFS
|
||||||
|
|
||||||
|
```python
|
||||||
|
from collections import deque
|
||||||
|
|
||||||
|
position = [[0, 1], [1, 0], [0, -1], [-1, 0]] # 四个方向
|
||||||
|
count = 0
|
||||||
|
|
||||||
|
|
||||||
|
def bfs(grid, visited, x, y):
|
||||||
|
"""
|
||||||
|
广度优先搜索对陆地进行标记
|
||||||
|
"""
|
||||||
|
global count # 声明全局变量
|
||||||
|
que = deque()
|
||||||
|
que.append([x, y])
|
||||||
|
while que:
|
||||||
|
cur_x, cur_y = que.popleft()
|
||||||
|
for i, j in position:
|
||||||
|
next_x = cur_x + i
|
||||||
|
next_y = cur_y + j
|
||||||
|
# 下标越界,跳过
|
||||||
|
if next_x < 0 or next_x >= len(grid) or next_y < 0 or next_y >= len(grid[0]):
|
||||||
|
continue
|
||||||
|
if grid[next_x][next_y] == 1 and not visited[next_x][next_y]:
|
||||||
|
visited[next_x][next_y] = True
|
||||||
|
count += 1
|
||||||
|
que.append([next_x, next_y])
|
||||||
|
|
||||||
|
|
||||||
|
n, m = map(int, input().split())
|
||||||
|
# 邻接矩阵
|
||||||
|
grid = []
|
||||||
|
for i in range(n):
|
||||||
|
grid.append(list(map(int, input().split())))
|
||||||
|
visited = [[False] * m for _ in range(n)] # 访问表
|
||||||
|
|
||||||
|
result = 0 # 记录最终结果
|
||||||
|
for i in range(n):
|
||||||
|
for j in range(m):
|
||||||
|
if grid[i][j] == 1 and not visited[i][j]:
|
||||||
|
count = 1
|
||||||
|
visited[i][j] = True
|
||||||
|
bfs(grid, visited, i, j)
|
||||||
|
res = max(result, count)
|
||||||
|
|
||||||
|
print(result)
|
||||||
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
Reference in New Issue
Block a user