Skip to content

Commit 0e832e9

Browse files
committed
change js to md
1 parent 2825dbc commit 0e832e9

26 files changed

+1939
-0
lines changed

001-100/61. Rotate List.md

+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 61. Rotate List
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Linked List, Two Pointers.
5+
- Similar Questions: Rotate Array, Split Linked List in Parts.
6+
7+
## Problem
8+
9+
Given a linked list, rotate the list to the right by **k** places, where **k** is non-negative.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: 1->2->3->4->5->NULL, k = 2
15+
Output: 4->5->1->2->3->NULL
16+
Explanation:
17+
rotate 1 steps to the right: 5->1->2->3->4->NULL
18+
rotate 2 steps to the right: 4->5->1->2->3->NULL
19+
```
20+
21+
**Example 2:**
22+
23+
```
24+
Input: 0->1->2->NULL, k = 4
25+
Output: 2->0->1->NULL
26+
Explanation:
27+
rotate 1 steps to the right: 2->0->1->NULL
28+
rotate 2 steps to the right: 1->2->0->NULL
29+
rotate 3 steps to the right: 0->1->2->NULL
30+
rotate 4 steps to the right: 2->0->1->NULL
31+
```
32+
33+
## Solution
34+
35+
```javascript
36+
/**
37+
* Definition for singly-linked list.
38+
* function ListNode(val) {
39+
* this.val = val;
40+
* this.next = null;
41+
* }
42+
*/
43+
/**
44+
* @param {ListNode} head
45+
* @param {number} k
46+
* @return {ListNode}
47+
*/
48+
var rotateRight = function(head, k) {
49+
var count = 1;
50+
var last = head;
51+
var now = head;
52+
53+
if (!head || !head.next) return head;
54+
55+
while (last.next) {
56+
last = last.next;
57+
count++;
58+
}
59+
60+
k %= count;
61+
62+
if (k === 0) return head;
63+
64+
while (k < count - 1) {
65+
now = now.next;
66+
k++;
67+
}
68+
69+
last.next = head;
70+
head = now.next;
71+
now.next = null;
72+
73+
return head;
74+
};
75+
```
76+
77+
**Explain:**
78+
79+
1. 拿到长度 count 和最后一个 last
80+
2. k %= count
81+
3. 找到新的最后一个 newLast,last.next = head,head = newLast.next,newLast.next = null
82+
4. return head
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(n).
87+
* Space complexity : O(1).

001-100/62. Unique Paths.md

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# 62. Unique Paths
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming.
5+
- Similar Questions: Unique Paths II, Minimum Path Sum, Dungeon Game.
6+
7+
## Problem
8+
9+
A robot is located at the top-left corner of a **m** x **n** grid (marked 'Start' in the diagram below).
10+
11+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
12+
13+
How many possible unique paths are there?
14+
15+
![](https://leetcode.com/static/images/problemset/robot_maze.png)
16+
17+
Above is a 7 x 3 grid. How many possible unique paths are there?
18+
19+
**Note:** **m** and **n** will be at most 100.
20+
21+
**Example 1:**
22+
23+
```
24+
Input: m = 3, n = 2
25+
Output: 3
26+
Explanation:
27+
From the top-left corner, there are a total of 3 ways to reach the bottom-right corner:
28+
1. Right -> Right -> Down
29+
2. Right -> Down -> Right
30+
3. Down -> Right -> Right
31+
```
32+
33+
**Example 2:**
34+
35+
```
36+
Input: m = 7, n = 3
37+
Output: 28
38+
```
39+
40+
## Solution
41+
42+
```javascript
43+
/**
44+
* @param {number} m
45+
* @param {number} n
46+
* @return {number}
47+
*/
48+
var uniquePaths = function(m, n) {
49+
var dp = Array(m);
50+
if (!m || !n) return 0;
51+
for (var i = 0; i < m; i++) {
52+
dp[i] = Array(n);
53+
for (var j = 0; j < n; j++) {
54+
if (j > 0 && i > 0) dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
55+
else if (j > 0 && i === 0) dp[i][j] = dp[i][j - 1];
56+
else if (j === 0 && i > 0) dp[i][j] = dp[i - 1][j];
57+
else dp[i][j] = 1;
58+
}
59+
}
60+
return dp[m - 1][n - 1];
61+
};
62+
```
63+
64+
**Explain:**
65+
66+
`dp[i][j]` 代表到达该点的路径数量。该点可以从左边点或上边点到达
67+
也就是 `dp[i][j] = dp[i - 1][j] + dp[i][j - 1]`
68+
69+
**Complexity:**
70+
71+
* Time complexity : O(m*n).
72+
* Space complexity : O(m*n).

001-100/63. Unique Paths II.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 63. Unique Paths II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming.
5+
- Similar Questions: Unique Paths.
6+
7+
## Problem
8+
9+
A robot is located at the top-left corner of a **m** x **n** grid (marked 'Start' in the diagram below).
10+
11+
The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
12+
13+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
14+
15+
![](https://leetcode.com/static/images/problemset/robot_maze.png)
16+
17+
An obstacle and empty space is marked as ```1``` and ```0``` respectively in the grid.
18+
19+
**Note:** **m** and **n** will be at most 100.
20+
21+
**Example 1:**
22+
23+
```
24+
Input:
25+
[
26+
  [0,0,0],
27+
  [0,1,0],
28+
  [0,0,0]
29+
]
30+
Output: 2
31+
Explanation:
32+
There is one obstacle in the middle of the 3x3 grid above.
33+
There are two ways to reach the bottom-right corner:
34+
1. Right -> Right -> Down -> Down
35+
2. Down -> Down -> Right -> Right
36+
```
37+
38+
## Solution
39+
40+
```javascript
41+
/**
42+
* @param {number[][]} obstacleGrid
43+
* @return {number}
44+
*/
45+
var uniquePathsWithObstacles = function(obstacleGrid) {
46+
var m = obstacleGrid.length;
47+
var n = (obstacleGrid[0] || []).length;
48+
var dp = Array(m);
49+
var left = 0;
50+
var top = 0;
51+
52+
if (!m || !n) return 0;
53+
54+
for (var i = 0; i < m; i++) {
55+
dp[i] = Array(n);
56+
for (var j = 0; j < n; j++) {
57+
left = (j === 0 || obstacleGrid[i][j - 1] === 1) ? 0 : dp[i][j - 1];
58+
top = (i === 0 || obstacleGrid[i - 1][j] === 1) ? 0 : dp[i - 1][j];
59+
dp[i][j] = obstacleGrid[i][j] === 1 ? 0 : ((i === 0 && j === 0) ? 1 : (left + top));
60+
}
61+
}
62+
63+
return dp[m - 1][n - 1];
64+
};
65+
```
66+
67+
**Explain:**
68+
69+
`dp[i][j]` 代表到达该点的路径数量。该点可以从左边点或上边点到达
70+
也就是 `dp[i][j] = dp[i - 1][j] + dp[i][j - 1]`
71+
72+
考虑特殊情况:
73+
74+
1. 该点为障碍,`dp[i][j] = 0`;
75+
2. 左边点为障碍或不存在,`left = 0`;
76+
3. 上边点点为障碍或不存在,`top = 0`;
77+
4. 左边点与上边点均不存在,即起点,`dp[i][j] = 1`;
78+
79+
**Complexity:**
80+
81+
* Time complexity : O(m*n).
82+
* Space complexity : O(m*n).

001-100/64. Minimum Path Sum.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# 64. Minimum Path Sum
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Dynamic Programming.
5+
- Similar Questions: Unique Paths, Dungeon Game, Cherry Pickup.
6+
7+
## Problem
8+
9+
Given a **m** x **n** grid filled with non-negative numbers, find a path from top left to bottom right which **minimizes** the sum of all numbers along its path.
10+
11+
**Note:** You can only move either down or right at any point in time.
12+
13+
**Example:**
14+
15+
```
16+
Input:
17+
[
18+
  [1,3,1],
19+
[1,5,1],
20+
[4,2,1]
21+
]
22+
Output: 7
23+
Explanation: Because the path 1→3→1→1→1 minimizes the sum.
24+
```
25+
26+
## Solution
27+
28+
```javascript
29+
/**
30+
* @param {number[][]} grid
31+
* @return {number}
32+
*/
33+
var minPathSum = function(grid) {
34+
var m = grid.length;
35+
var n = (grid[0] || []).length;
36+
var dp = Array(m);
37+
var left = 0;
38+
var top = 0;
39+
40+
if (!m || !n) return 0;
41+
42+
for (var i = 0; i < m; i++) {
43+
dp[i] = Array(n);
44+
for (var j = 0; j < n; j++) {
45+
top = i === 0 ? Number.MAX_SAFE_INTEGER : dp[i - 1][j];
46+
left = j === 0 ? Number.MAX_SAFE_INTEGER : dp[i][j - 1];
47+
dp[i][j] = grid[i][j] + (i === 0 && j === 0 ? 0 : Math.min(left, top));
48+
}
49+
}
50+
51+
return dp[m - 1][n - 1];
52+
};
53+
```
54+
55+
**Explain:**
56+
57+
`dp[i][j]` 代表到达此位置的最小 `sum`
58+
59+
`dp[i][j] = min(dp[i - 1][j] + dp[i][j - 1]) + grid[i][j]`;
60+
61+
**Complexity:**
62+
63+
* Time complexity : O(m*n).
64+
* Space complexity : O(m*n).

001-100/65. Valid Number.md

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 65. Valid Number
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Math, String.
5+
- Similar Questions: String to Integer (atoi).
6+
7+
## Problem
8+
9+
Validate if a given string is numeric.
10+
11+
Some examples:
12+
```"0"``` => ```true```
13+
```" 0.1 "``` => ```true```
14+
```"abc"``` => ```false```
15+
```"1 a"``` => ```false```
16+
```"2e10"``` => ```true```
17+
18+
**Note:** It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
19+
20+
**Update (2015-02-10):**
21+
The signature of the ```C++``` function had been updated. If you still see your function signature accepts a ```const char *``` argument, please click the reload button to reset your code definition.
22+
23+
## Solution
24+
25+
```javascript
26+
/**
27+
* @param {string} s
28+
* @return {boolean}
29+
*/
30+
var isNumber = function(s) {
31+
var state = [
32+
{},
33+
{'blank': 1, 'sign': 2, 'digit':3, '.':4},
34+
{'digit':3, '.':4},
35+
{'digit':3, '.':5, 'e':6, 'blank':9},
36+
{'digit':5},
37+
{'digit':5, 'e':6, 'blank':9},
38+
{'sign':7, 'digit':8},
39+
{'digit':8},
40+
{'digit':8, 'blank':9},
41+
{'blank':9}
42+
];
43+
var validState = [3, 5, 8, 9];
44+
var currentState = 1;
45+
var len = s.length;
46+
var str = '';
47+
var type = '';
48+
for (var i = 0; i < len; i++) {
49+
str = s[i];
50+
if (str >= '0' && str <= '9') {
51+
type = 'digit';
52+
} else if (str === '+' || str === '-') {
53+
type = 'sign';
54+
} else if (str === ' ') {
55+
type = 'blank';
56+
} else {
57+
type = str;
58+
}
59+
if (state[currentState][type] === undefined) {
60+
return false;
61+
} else {
62+
currentState = state[currentState][type];
63+
}
64+
}
65+
if (validState.indexOf(currentState) === -1) {
66+
return false;
67+
} else {
68+
return true;
69+
}
70+
};
71+
```
72+
73+
**Explain:**
74+
75+
[DFA 确定有限状态自动机](https://leetcode.com/problems/valid-number/discuss/23728/A-simple-solution-in-Python-based-on-DFA)
76+
77+
**Complexity:**
78+
79+
* Time complexity : O(n).
80+
* Space complexity : O(1).

0 commit comments

Comments
 (0)