Skip to content

Commit b558e18

Browse files
author
lucifer
committed
2 parents 4946d4b + 9a6e5ed commit b558e18

File tree

3 files changed

+49
-86
lines changed

3 files changed

+49
-86
lines changed

problems/11.container-with-most-water.md

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -71,53 +71,13 @@ eg:
7171
JavaScript Code:
7272

7373
```js
74-
/*
75-
* @lc app=leetcode id=11 lang=javascript
76-
*
77-
* [11] Container With Most Water
78-
*
79-
* https://leetcode.com/problems/container-with-most-water/description/
80-
*
81-
* algorithms
82-
* Medium (42.86%)
83-
* Total Accepted: 344.3K
84-
* Total Submissions: 790.1K
85-
* Testcase Example: '[1,8,6,2,5,4,8,3,7]'
86-
*
87-
* Given n non-negative integers a1, a2, ..., an , where each represents a
88-
* point at coordinate (i, ai). n vertical lines are drawn such that the two
89-
* endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together
90-
* with x-axis forms a container, such that the container contains the most
91-
* water.
92-
*
93-
* Note: You may not slant the container and n is at least 2.
94-
*
95-
*
96-
*
97-
*
98-
*
99-
* The above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In
100-
* this case, the max area of water (blue section) the container can contain is
101-
* 49.
102-
*
103-
*
104-
*
105-
* Example:
106-
*
107-
*
108-
* Input: [1,8,6,2,5,4,8,3,7]
109-
* Output: 49
110-
*
111-
*/
11274
/**
11375
* @param {number[]} height
11476
* @return {number}
11577
*/
11678
var maxArea = function(height) {
11779
if (!height || height.length <= 1) return 0;
11880

119-
// 双指针来进行优化
120-
// 时间复杂度是O(n)
12181
let leftPos = 0;
12282
let rightPos = height.length - 1;
12383
let max = 0;
@@ -154,3 +114,12 @@ public:
154114
}
155115
};
156116
```
117+
118+
***复杂度分析***
119+
- 时间复杂度:$O(N)$
120+
- 空间复杂度:$O(1)$
121+
122+
123+
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解
124+
125+
![](https://pic.leetcode-cn.com/89ef69abbf02a2957838499a96ce3fbb26830aae52e3ab90392e328c2670cddc-file_1581478989502)

problems/200.number-of-islands.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,37 @@ Output: 3
5353

5454
## 代码
5555

56-
* 语言支持:JS, python3
56+
* 语言支持:JS, python3,Java
57+
58+
Java Code:
59+
60+
```java
61+
public int numIslands(char[][] grid) {
62+
if (grid == null || grid.length == 0 || grid[0].length == 0) return 0;
63+
64+
int count = 0;
65+
for (int row = 0; row < grid.length; row++) {
66+
for (int col = 0; col < grid[0].length; col++) {
67+
if (grid[row][col] == '1') {
68+
dfs(grid, row, col);
69+
count++;
70+
}
71+
}
72+
}
73+
return count;
74+
}
75+
76+
private void dfs(char[][] grid,int row,int col) {
77+
if (row<0||row== grid.length||col<0||col==grid[0].length||grid[row][col]!='1') {
78+
return;
79+
}
80+
grid[row][col] = '0';
81+
dfs(grid, row-1, col);
82+
dfs(grid, row+1, col);
83+
dfs(grid, row, col+1);
84+
dfs(grid, row, col-1);
85+
}
86+
```
5787

5888
Javascript Code:
5989
```js

problems/55.jump-game.md

Lines changed: 9 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ Explanation: You will always arrive at index 3 no matter what. Its maximum
2626

2727
## 思路
2828

29-
这道题目是一道典型的`回溯`类型题目。
30-
思路就是用一个变量记录当前能够到达的最大的索引,我们逐个遍历数组中的元素去更新这个索引。
31-
变量完成判断这个索引是否大于数组下表即可。
29+
这道题目是一道典型的`贪心`类型题目。思路就是用一个变量记录当前能够到达的最大的索引,我们逐个遍历数组中的元素去更新这个索引。变量完成判断这个索引是否大于数组下表即可。
3230
## 关键点解析
3331

3432
- 建模 (记录和更新当前位置能够到达的最大的索引即可)
@@ -38,48 +36,6 @@ Explanation: You will always arrive at index 3 no matter what. Its maximum
3836
* 语言支持: Javascript,Python3
3937

4038
```js
41-
42-
/*
43-
* @lc app=leetcode id=55 lang=javascript
44-
*
45-
* [55] Jump Game
46-
*
47-
* https://leetcode.com/problems/jump-game/description/
48-
*
49-
* algorithms
50-
* Medium (31.38%)
51-
* Total Accepted: 252.4K
52-
* Total Submissions: 797.2K
53-
* Testcase Example: '[2,3,1,1,4]'
54-
*
55-
* Given an array of non-negative integers, you are initially positioned at the
56-
* first index of the array.
57-
*
58-
* Each element in the array represents your maximum jump length at that
59-
* position.
60-
*
61-
* Determine if you are able to reach the last index.
62-
*
63-
* Example 1:
64-
*
65-
*
66-
* Input: [2,3,1,1,4]
67-
* Output: true
68-
* Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last
69-
* index.
70-
*
71-
*
72-
* Example 2:
73-
*
74-
*
75-
* Input: [3,2,1,0,4]
76-
* Output: false
77-
* Explanation: You will always arrive at index 3 no matter what. Its
78-
* maximum
79-
* jump length is 0, which makes it impossible to reach the last index.
80-
*
81-
*
82-
*/
8339
/**
8440
* @param {number[]} nums
8541
* @return {boolean}
@@ -112,3 +68,11 @@ class Solution:
11268
return True
11369
return _max >= _len - 1
11470
```
71+
72+
***复杂度分析***
73+
- 时间复杂度:$O(N)$
74+
- 空间复杂度:$O(1)$
75+
76+
更多题解可以访问我的LeetCode题解仓库:https://github.com/azl397985856/leetcode 。 目前已经30K star啦。
77+
78+
大家也可以关注我的公众号《脑洞前端》获取更多更新鲜的LeetCode题解

0 commit comments

Comments
 (0)