Skip to content

Commit c978359

Browse files
committed
change js to md
1 parent 3aad115 commit c978359

23 files changed

+1699
-0
lines changed

001-100/38. Count and Say.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# 38. Count and Say
2+
3+
- Difficulty: Easy.
4+
- Related Topics: String.
5+
- Similar Questions: Encode and Decode Strings, String Compression.
6+
7+
## Problem
8+
9+
The count-and-say sequence is the sequence of integers with the first five terms as following:
10+
```
11+
1. 1
12+
2. 11
13+
3. 21
14+
4. 1211
15+
5. 111221
16+
```
17+
18+
```1``` is read off as ```"one 1"``` or ```11```.
19+
```11``` is read off as ```"two 1s"``` or ```21```.
20+
```21``` is read off as ```"one 2```, then ```one 1"``` or ```1211```.
21+
22+
Given an integer *n*, generate the *n*th term of the count-and-say sequence.
23+
24+
Note: Each term of the sequence of integers will be represented as a string.
25+
26+
**Example 1:**
27+
```
28+
Input: 1
29+
Output: "1"
30+
```
31+
32+
**Example 2:**
33+
```
34+
Input: 4
35+
Output: "1211"
36+
```
37+
38+
## Solution
39+
40+
```javascript
41+
/**
42+
* @param {number} n
43+
* @return {string}
44+
*/
45+
var countAndSay = function(n) {
46+
var str = '1';
47+
var tmp = '';
48+
var last = '';
49+
var count = 0;
50+
var len = 0;
51+
52+
for (var i = 1; i < n; i++) {
53+
tmp = '';
54+
last = '';
55+
count = 0;
56+
len = str.length;
57+
58+
for (var j = 0; j < len; j++) {
59+
if (last === '') {
60+
last = str[j];
61+
count = 1;
62+
continue;
63+
}
64+
if (str[j] === last) {
65+
count += 1;
66+
} else {
67+
tmp += '' + count + last;
68+
last = str[j];
69+
count = 1;
70+
}
71+
}
72+
73+
if (last) {
74+
tmp += '' + count + last;
75+
}
76+
77+
str = tmp;
78+
}
79+
80+
return str;
81+
};
82+
```
83+
84+
**Explain:**
85+
86+
nope.
87+
88+
**Complexity:**
89+
90+
* Time complexity :
91+
* Space complexity :

001-100/39. Combination Sum.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# 39. Combination Sum
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Backtracking.
5+
- Similar Questions: Letter Combinations of a Phone Number, Combination Sum II, Combinations, Combination Sum III, Factor Combinations, Combination Sum IV.
6+
7+
## Problem
8+
9+
Given a **set** of candidate numbers (```candidates```) **(without duplicates)** and a target number (```target```), find all unique combinations in ```candidates``` where the candidate numbers sums to ```target```.
10+
11+
The **same** repeated number may be chosen from ```candidates``` unlimited number of times.
12+
13+
**Note:**
14+
15+
- All numbers (including ```target```) will be positive integers.
16+
- The solution set must not contain duplicate combinations.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: candidates = [2,3,6,7], target = 7,
22+
A solution set is:
23+
[
24+
[7],
25+
[2,2,3]
26+
]
27+
```
28+
29+
**Example 2:**
30+
31+
```
32+
Input: candidates = [2,3,5], target = 8,
33+
A solution set is:
34+
[
35+
  [2,2,2,2],
36+
  [2,3,3],
37+
  [3,5]
38+
]
39+
```
40+
41+
## Solution
42+
43+
```javascript
44+
/**
45+
* @param {number[]} candidates
46+
* @param {number} target
47+
* @return {number[][]}
48+
*/
49+
var combinationSum = function(candidates, target) {
50+
var res = [];
51+
var len = candidates.length;
52+
candidates.sort((a, b) => (a - b));
53+
dfs(res, [], 0, len, candidates, target);
54+
return res;
55+
};
56+
57+
var dfs = function (res, stack, index, len, candidates, target) {
58+
var tmp = null;
59+
if (target < 0) return;
60+
if (target === 0) return res.push(stack);
61+
for (var i = index; i < len; i++) {
62+
if (candidates[i] > target) break;
63+
tmp = Array.from(stack);
64+
tmp.push(candidates[i]);
65+
dfs(res, tmp, i, len, candidates, target - candidates[i]);
66+
}
67+
};
68+
```
69+
70+
**Explain:**
71+
72+
对树进行深度优先的搜索。注意解法不能重复,即下次搜索从 index 开始
73+
74+
**Complexity:**
75+
76+
* Time complexity : O(n^2).
77+
* Space complexity : O(n^2).

001-100/40. Combination Sum II.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# 40. Combination Sum II
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Backtracking.
5+
- Similar Questions: Combination Sum.
6+
7+
## Problem
8+
9+
Given a collection of candidate numbers (```candidates```) and a target number (```target```), find all unique combinations in ```candidates``` where the candidate numbers sums to ```target```.
10+
11+
Each number in ```candidates``` may only be used **once** in the combination.
12+
13+
**Note:**
14+
15+
- All numbers (including ```target```) will be positive integers.
16+
- The solution set must not contain duplicate combinations.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: candidates = [10,1,2,7,6,1,5], target = 8,
22+
A solution set is:
23+
[
24+
[1, 7],
25+
[1, 2, 5],
26+
[2, 6],
27+
[1, 1, 6]
28+
]
29+
```
30+
31+
**Example 2:**
32+
33+
```
34+
Input: candidates = [2,5,2,1,2], target = 5,
35+
A solution set is:
36+
[
37+
  [1,2,2],
38+
  [5]
39+
]
40+
```
41+
42+
## Solution
43+
44+
```javascript
45+
/**
46+
* @param {number[]} candidates
47+
* @param {number} target
48+
* @return {number[][]}
49+
*/
50+
var combinationSum2 = function(candidates, target) {
51+
var res = [];
52+
var len = candidates.length;
53+
candidates.sort((a, b) => (a - b));
54+
dfs(res, [], 0, len, candidates, target);
55+
return res;
56+
};
57+
58+
var dfs = function (res, stack, index, len, candidates, target) {
59+
var tmp = null;
60+
if (target < 0) return;
61+
if (target === 0) return res.push(stack);
62+
for (var i = index; i < len; i++) {
63+
if (candidates[i] > target) break;
64+
if (i > index && candidates[i] === candidates[i - 1]) continue;
65+
tmp = Array.from(stack);
66+
tmp.push(candidates[i]);
67+
dfs(res, tmp, i + 1, len, candidates, target - candidates[i]);
68+
}
69+
};
70+
```
71+
72+
**Explain:**
73+
74+
与之前一题不同的地方是:
75+
76+
1. 候选数字可能有重复的
77+
2. 单个候选数字不能重复使用
78+
79+
**Complexity:**
80+
81+
* Time complexity : O(n^2).
82+
* Space complexity : O(n^2).

001-100/41. First Missing Positive.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# 41. First Missing Positive
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array.
5+
- Similar Questions: Missing Number, Find the Duplicate Number, Find All Numbers Disappeared in an Array, Couples Holding Hands.
6+
7+
## Problem
8+
9+
Given an unsorted integer array, find the smallest missing positive integer.
10+
11+
**Example 1:**
12+
13+
```
14+
Input: [1,2,0]
15+
Output: 3
16+
```
17+
18+
**Example 2:**
19+
20+
```
21+
Input: [3,4,-1,1]
22+
Output: 2
23+
```
24+
25+
**Example 3:**
26+
27+
```
28+
Input: [7,8,9,11,12]
29+
Output: 1
30+
```
31+
32+
**Note:**
33+
34+
Your algorithm should run in **O**(**n**) time and uses constant extra space.
35+
36+
## Solution
37+
38+
```javascript
39+
/**
40+
* @param {number[]} nums
41+
* @return {number}
42+
*/
43+
var firstMissingPositive = function(nums) {
44+
var len = nums.length;
45+
var tmp = 0;
46+
var i = 0;
47+
while (i < len) {
48+
tmp = nums[i];
49+
if (tmp > 0 && tmp !== i + 1 && tmp !== nums[tmp - 1]) swap(nums, i, tmp - 1);
50+
else i++;
51+
}
52+
for (var j = 0; j < len; j++) {
53+
if (nums[j] !== j + 1) return j + 1;
54+
}
55+
return len + 1;
56+
};
57+
58+
var swap = function (arr, i, j) {
59+
var tmp = arr[i];
60+
arr[i] = arr[j];
61+
arr[j] = tmp;
62+
};
63+
```
64+
65+
**Explain:**
66+
67+
循环把 `nums[i]` 放到 `nums[nums[i] - 1]` 那里,最后 `nums[i] !== i + 1` 就是不对的
68+
69+
即比如 `[3, 4, -2, 1]` => `[1, -2, 3, 4]`,即缺 `2`
70+
71+
**Complexity:**
72+
73+
* Time complexity : O(n).
74+
* Space complexity : O(1).

001-100/42. Trapping Rain Water.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 42. Trapping Rain Water
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Array, Two Pointers, Stack.
5+
- Similar Questions: Container With Most Water, Product of Array Except Self, Trapping Rain Water II, Pour Water.
6+
7+
## Problem
8+
9+
Given **n** non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.
10+
11+
![](http://www.leetcode.com/static/images/problemset/rainwatertrap.png)
12+
13+
The above elevation map is represented by array [0,1,0,2,1,0,1,3,2,1,2,1]. In this case, 6 units of rain water (blue section) are being trapped. **Thanks Marcos** for contributing this image!
14+
15+
**Example:**
16+
17+
```
18+
Input: [0,1,0,2,1,0,1,3,2,1,2,1]
19+
Output: 6
20+
```
21+
22+
## Solution
23+
24+
```javascript
25+
var trap = function(height) {
26+
var res = 0;
27+
var left = 0;
28+
var right = height.length - 1;
29+
var leftMax = 0;
30+
var rightMax = 0;
31+
32+
while (left < right) {
33+
if (height[left] < height[right]) {
34+
if (height[left] >= leftMax) {
35+
leftMax = height[left];
36+
} else {
37+
res += leftMax - height[left];
38+
}
39+
left++;
40+
} else {
41+
if (height[right] >= rightMax) {
42+
rightMax = height[right];
43+
} else {
44+
res += rightMax - height[right];
45+
}
46+
right--;
47+
}
48+
}
49+
50+
return res;
51+
};
52+
```
53+
54+
**Explain:**
55+
56+
双指针
57+
58+
**Complexity:**
59+
60+
* Time complexity : O(n).
61+
* Space complexity : O(1).

0 commit comments

Comments
 (0)