mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #2268 from jinjjm/master
200岛屿数量、695岛屿面积、797所有可能路径 js版
This commit is contained in:
@ -239,6 +239,42 @@ class Solution:
|
|||||||
visited[next_i][next_j] = True
|
visited[next_i][next_j] = True
|
||||||
|
|
||||||
```
|
```
|
||||||
|
### JavaScript
|
||||||
|
```javascript
|
||||||
|
var numIslands = function (grid) {
|
||||||
|
let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向
|
||||||
|
let bfs = (grid, visited, x, y) => {
|
||||||
|
let queue = [];
|
||||||
|
queue.push([x, y]);
|
||||||
|
visited[x][y] = true;
|
||||||
|
while (queue.length) {
|
||||||
|
let top = queue.shift();//取出队列头部元素
|
||||||
|
console.log(top)
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
let nextX = top[0] + dir[i][0]
|
||||||
|
let nextY = top[1] + dir[i][1]
|
||||||
|
if (nextX < 0 || nextX >= grid.length || nextY < 0 || nextY >= grid[0].length)
|
||||||
|
continue;
|
||||||
|
if (!visited[nextX][nextY] && grid[nextX][nextY] === "1") {
|
||||||
|
queue.push([nextX, nextY])
|
||||||
|
visited[nextX][nextY] = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false))
|
||||||
|
let res = 0
|
||||||
|
for (let i = 0; i < grid.length; i++) {
|
||||||
|
for (let j = 0; j < grid[i].length; j++) {
|
||||||
|
if (!visited[i][j] && grid[i][j] === "1") {
|
||||||
|
++res;
|
||||||
|
bfs(grid, visited, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
|
@ -278,6 +278,39 @@ class Solution:
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
```
|
```
|
||||||
|
### JavaScript
|
||||||
|
|
||||||
|
```javascript
|
||||||
|
var numIslands = function (grid) {
|
||||||
|
let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向
|
||||||
|
|
||||||
|
let dfs = (grid, visited, x, y) => {
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
let nextX = x + dir[i][0]
|
||||||
|
let 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false))
|
||||||
|
|
||||||
|
let res = 0
|
||||||
|
for (let i = 0; i < grid.length; i++) {
|
||||||
|
for (let j = 0; j < grid[i].length; j++) {
|
||||||
|
if (!visited[i][j] && grid[i][j] === "1") {
|
||||||
|
++res;
|
||||||
|
visited[i][j] = true;
|
||||||
|
dfs(grid, visited, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
|
@ -391,6 +391,44 @@ class Solution:
|
|||||||
self.dfs(grid, visited, new_x, new_y)
|
self.dfs(grid, visited, new_x, new_y)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### JavaScript
|
||||||
|
```javascript
|
||||||
|
var maxAreaOfIsland = function (grid) {
|
||||||
|
let dir = [[0, 1], [1, 0], [-1, 0], [0, -1]]; // 四个方向
|
||||||
|
|
||||||
|
let visited = new Array(grid.length).fill().map(() => Array(grid[0].length).fill(false))
|
||||||
|
|
||||||
|
let dfs = (grid, visited, x, y, m) => {
|
||||||
|
for (let i = 0; i < 4; i++) {
|
||||||
|
let nextX = x + dir[i][0]
|
||||||
|
let 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
|
||||||
|
m = dfs(grid, visited, nextX, nextY,m+1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
let max = 0
|
||||||
|
|
||||||
|
for (let i = 0; i < grid.length; i++) {
|
||||||
|
for (let j = 0; j < grid[i].length; j++) {
|
||||||
|
if (!visited[i][j] && grid[i][j] === 1) {
|
||||||
|
// 深度优先
|
||||||
|
visited[i][j] = true;
|
||||||
|
let m = dfs(grid, visited, i, j, 1);
|
||||||
|
if (m > max) max = m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
dfs: 版本一
|
dfs: 版本一
|
||||||
@ -530,7 +568,6 @@ impl Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
<p align="center">
|
<p align="center">
|
||||||
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
|
||||||
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
<img src="../pics/网站星球宣传海报.jpg" width="1000"/>
|
||||||
|
@ -217,6 +217,30 @@ class Solution:
|
|||||||
self.path.pop() # 回溯
|
self.path.pop() # 回溯
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### JavaScript
|
||||||
|
```javascript
|
||||||
|
var allPathsSourceTarget = function(graph) {
|
||||||
|
let res=[],path=[]
|
||||||
|
|
||||||
|
function dfs(graph,start){
|
||||||
|
if(start===graph.length-1){
|
||||||
|
res.push([...path])
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
for(let i=0;i<graph[start].length;i++){
|
||||||
|
path.push(graph[start][i])
|
||||||
|
dfs(graph,graph[start][i])
|
||||||
|
path.pop()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
path.push(0)
|
||||||
|
dfs(graph,0)
|
||||||
|
return res
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -247,6 +271,7 @@ func allPathsSourceTarget(graph [][]int) [][]int {
|
|||||||
|
|
||||||
### Rust
|
### Rust
|
||||||
|
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
impl Solution {
|
impl Solution {
|
||||||
pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
pub fn all_paths_source_target(graph: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
||||||
|
Reference in New Issue
Block a user