mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
Merge branch 'master' into master
This commit is contained in:
@ -230,7 +230,25 @@ class Solution:
|
|||||||
|
|
||||||
```
|
```
|
||||||
### Go
|
### Go
|
||||||
|
贪心法
|
||||||
|
```go
|
||||||
|
func maxSubArray(nums []int) int {
|
||||||
|
max := nums[0]
|
||||||
|
count := 0
|
||||||
|
|
||||||
|
for i := 0; i < len(nums); i++{
|
||||||
|
count += nums[i]
|
||||||
|
if count > max{
|
||||||
|
max = count
|
||||||
|
}
|
||||||
|
if count < 0 {
|
||||||
|
count = 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max
|
||||||
|
}
|
||||||
|
```
|
||||||
|
动态规划
|
||||||
```go
|
```go
|
||||||
func maxSubArray(nums []int) int {
|
func maxSubArray(nums []int) int {
|
||||||
maxSum := nums[0]
|
maxSum := nums[0]
|
||||||
|
@ -348,6 +348,7 @@ class Solution:
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Go
|
### Go
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -439,6 +440,70 @@ func max827(x, y int) int {
|
|||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### JavaScript
|
||||||
|
|
||||||
|
```JavaScript
|
||||||
|
|
||||||
|
var largestIsland = function(grid) {
|
||||||
|
let res = 0;
|
||||||
|
const m = grid.length;
|
||||||
|
const n = grid[0].length;
|
||||||
|
const tag = new Array(n).fill().map(_ => new Array(m).fill(0));
|
||||||
|
const area = new Map();
|
||||||
|
const dir = [[0,1],[0,-1],[1,0],[-1,0]];
|
||||||
|
const dfs = (grid,tag,x,y,mark) => {
|
||||||
|
let res = 1;
|
||||||
|
tag[x][y] = mark;
|
||||||
|
for(let i = 0; i < dir.length; i++) {
|
||||||
|
let nextX = x + dir[i][0];
|
||||||
|
let nextY = y + dir[i][1];
|
||||||
|
if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if(grid[nextX][nextY] === 1 && tag[nextX][nextY] === 0) {
|
||||||
|
res += dfs(grid,tag,nextX,nextY,mark);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
let mark = 2;
|
||||||
|
//将岛屿用mark标记
|
||||||
|
for(let i = 0; i < m; i++) {
|
||||||
|
for(let j = 0; j < n; j++) {
|
||||||
|
if(grid[i][j] === 1 && tag[i][j] === 0) {
|
||||||
|
area.set(mark,dfs(grid,tag,i,j,mark));
|
||||||
|
res = Math.max(res,area.get(mark));
|
||||||
|
mark++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//将一个非岛屿格子变为岛屿
|
||||||
|
for(let i = 0; i < m; i++) {
|
||||||
|
for(let j = 0; j < n; j++) {
|
||||||
|
if(grid[i][j] === 0) {
|
||||||
|
let z = 1;
|
||||||
|
const connected = new Set();
|
||||||
|
for(let k = 0; k < dir.length; k++) {
|
||||||
|
let nextX = i + dir[k][0];
|
||||||
|
let nextY = j + dir[k][1];
|
||||||
|
if(nextX < 0 || nextX >= m || nextY < 0 || nextY >= n || tag[nextX][nextY] === 0 || connected.has(tag[nextX][nextY])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
z += area.get(tag[nextX][nextY]);
|
||||||
|
connected.add(tag[nextX][nextY]);
|
||||||
|
}
|
||||||
|
res = Math.max(res,z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
<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"/>
|
||||||
|
Reference in New Issue
Block a user