Skip to content

Commit 1acf84d

Browse files
committed
finish 29,30,31,76,118,155
1 parent 8adfe0d commit 1acf84d

6 files changed

+559
-4
lines changed

001-100/29. Divide Two Integers.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,42 @@ Output: -2
3535
## Solution
3636

3737
```javascript
38-
38+
/**
39+
* @param {number} dividend
40+
* @param {number} divisor
41+
* @return {number}
42+
*/
43+
var divide = function(dividend, divisor) {
44+
var did = Math.abs(dividend);
45+
var dis = Math.abs(divisor);
46+
var sign = (divisor > 0 && dividend > 0) || (divisor < 0 && dividend < 0);
47+
var res = 0;
48+
var arr = [dis];
49+
50+
if (dividend === 0 || did < dis) return 0;
51+
if (divisor === -1 && dividend === -2147483648) return 2147483647;
52+
if (dis === 1) return divisor > 0 ? dividend : -dividend;
53+
54+
while (arr[arr.length - 1] < did) arr.push(arr[arr.length - 1] + arr[arr.length - 1]);
55+
56+
for (var i = arr.length - 1; i >= 0; i--) {
57+
if (did >= arr[i]) {
58+
did -= arr[i];
59+
res += i === 0 ? 1 : 2 << (i - 1);
60+
}
61+
}
62+
63+
return sign ? res : -res;
64+
};
3965
```
4066

4167
**Explain:**
4268

43-
nope.
69+
1. 要注意 `-2147483648 / -1` 越界的情况
70+
2. 核心就是 `dividend -= divisor << i; result += 2 << (i - 1);`
71+
3. 其它语言有 `long``long long` 类型可以保证 `divisor << i` 不越界,但是 js 没有。比如 `2 << 29``1073741824``2 << 30` 就越界了,不会得到预期的结果。我这里是用加法提前计算好 `divisor << i`
4472

4573
**Complexity:**
4674

47-
* Time complexity : O(n).
48-
* Space complexity : O(n).
75+
* Time complexity : O(log(n)).
76+
* Space complexity : O(log(n)).
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# 30. Substring with Concatenation of All Words
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Hash Table, Two Pointers, String.
5+
- Similar Questions: Minimum Window Substring.
6+
7+
## Problem
8+
9+
You are given a string, **s**, and a list of words, **words**, that are all of the same length. Find all starting indices of substring(s) in **s** that is a concatenation of each word in **words** exactly once and without any intervening characters.
10+
11+
**Example 1:**
12+
13+
```
14+
Input:
15+
s = "barfoothefoobarman",
16+
words = ["foo","bar"]
17+
Output: [0,9]
18+
Explanation: Substrings starting at index 0 and 9 are "barfoor" and "foobar" respectively.
19+
The output order does not matter, returning [9,0] is fine too.
20+
```
21+
22+
**Example 2:**
23+
24+
```
25+
Input:
26+
s = "wordgoodstudentgoodword",
27+
words = ["word","student"]
28+
Output: []
29+
```
30+
31+
## Solution
32+
33+
```javascript
34+
/**
35+
* @param {string} s
36+
* @param {string[]} words
37+
* @return {number[]}
38+
*/
39+
var findSubstring = function(s, words) {
40+
var sLen = s.length;
41+
var wLen = words.length;
42+
var wordLen = (words[0] || '').length;
43+
44+
if (!sLen || !wLen || !wordLen) return [];
45+
46+
var count = 0;
47+
var tmp = '';
48+
var map1 = {};
49+
var map2 = {};
50+
var res = [];
51+
52+
for (var i = 0; i < wLen; i++) {
53+
map1[words[i]] = (map1[words[i]] || 0) + 1;
54+
}
55+
56+
out: for (var j = 0; j <= sLen - (wLen * wordLen); j++) {
57+
map2 = {};
58+
count = 0;
59+
while (count < wLen) {
60+
tmp = s.substr(j + (count * wordLen), wordLen);
61+
if (map1[tmp] === undefined || map1[tmp] === map2[tmp]) continue out;
62+
map2[tmp] = (map2[tmp] || 0) + 1;
63+
count++;
64+
}
65+
res.push(j);
66+
}
67+
68+
return res;
69+
};
70+
```
71+
72+
**Explain:**
73+
74+
nope.
75+
76+
**Complexity:**
77+
78+
* Time complexity : O(m*n).
79+
* Space complexity : O(m).

001-100/31. Next Permutation.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 31. Next Permutation
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array.
5+
- Similar Questions: Permutations, Permutations II, Permutation Sequence, Palindrome Permutation II.
6+
7+
## Problem
8+
9+
Implement **next permutation**, which rearranges numbers into the lexicographically next greater permutation of numbers.
10+
11+
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
12+
13+
The replacement must be **in-place** and use only constant extra memory.
14+
15+
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
16+
17+
```1,2,3``````1,3,2```
18+
19+
```3,2,1``````1,2,3```
20+
21+
```1,1,5``````1,5,1```
22+
23+
## Solution
24+
25+
```javascript
26+
/**
27+
* @param {number[]} nums
28+
* @return {void} Do not return anything, modify nums in-place instead.
29+
*/
30+
var nextPermutation = function(nums) {
31+
var len = nums.length;
32+
var i = len - 2;
33+
var j = len - 1;
34+
35+
while (i >= 0 && nums[i] >= nums[i + 1]) i--;
36+
37+
if (i >= 0) {
38+
while (j > i && nums[j] <= nums[i]) j--;
39+
swap(nums, i, j);
40+
reverse(nums, i + 1, len - 1);
41+
} else {
42+
reverse(nums, 0, len - 1);
43+
}
44+
};
45+
46+
var swap = function (arr, from, to) {
47+
var tmp = arr[from];
48+
arr[from] = arr[to];
49+
arr[to] = tmp;
50+
};
51+
52+
var reverse = function (arr, start, end) {
53+
while (start < end) {
54+
swap(arr, start, end);
55+
start++;
56+
end--;
57+
}
58+
};
59+
```
60+
61+
**Explain:**
62+
63+
1. 从后往前找,直到 `nums[i] < nums[i + 1]`
64+
2. 找到 `i` 后,从后往前找,直到 `nums[j] > nums[i]`,交换 `nums[i]``nums[j]`,然后把 `i` 后的倒置一下
65+
3. 没找到 `i` 的话,直接倒置一下
66+
67+
**Complexity:**
68+
69+
* Time complexity : O(n).
70+
* Space complexity : O(1).
+87
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 76. Minimum Window Substring
2+
3+
- Difficulty: Hard.
4+
- Related Topics: Hash Table, Two Pointers, String.
5+
- Similar Questions: Substring with Concatenation of All Words, Minimum Size Subarray Sum, Sliding Window Maximum, Permutation in String, Smallest Range, Minimum Window Subsequence.
6+
7+
## Problem
8+
9+
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).
10+
11+
**Example:**
12+
13+
```
14+
Input: S = "ADOBECODEBANC", T = "ABC"
15+
Output: "BANC"
16+
```
17+
18+
**Note:**
19+
20+
- If there is no such window in S that covers all characters in T, return the empty string ```""```.
21+
- If there is such window, you are guaranteed that there will always be only one unique minimum window in S.
22+
23+
## Solution
24+
25+
```javascript
26+
/**
27+
* @param {string} s
28+
* @param {string} t
29+
* @return {string}
30+
*/
31+
var minWindow = function(s, t) {
32+
var map = {};
33+
var sLen = s.length;
34+
var tLen = t.length;
35+
var count = tLen;
36+
var min = Number.MAX_SAFE_INTEGER;
37+
var head = 0;
38+
var left = 0;
39+
var right = 0;
40+
41+
if (!sLen || !tLen) return '';
42+
43+
for (var i = 0; i < tLen; i++) {
44+
if (map[t[i]] === undefined) {
45+
map[t[i]] = 1
46+
} else {
47+
map[t[i]]++;
48+
}
49+
}
50+
51+
while (right < sLen) {
52+
if (map[s[right]] !== undefined) {
53+
if (map[s[right]] > 0) count--;
54+
map[s[right]]--;
55+
}
56+
57+
right++;
58+
59+
while (count === 0) {
60+
if (right - left < min) {
61+
min = right - left;
62+
head = left;
63+
}
64+
65+
if (map[s[left]] !== undefined) {
66+
if (map[s[left]] === 0) count++;
67+
map[s[left]]++;
68+
}
69+
70+
left++;
71+
}
72+
}
73+
74+
return min === Number.MAX_SAFE_INTEGER ? '' : s.substr(head, min);
75+
};
76+
```
77+
78+
**Explain:**
79+
80+
see [Here is a 10-line template that can solve most 'substring' problems](https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems).
81+
82+
用哈希表保存各字符的数量,双指针表示当前窗口,用计数器来判断是否符合条件。符合条件后,记录窗口位置、最小值;更新双指针、计数器。
83+
84+
**Complexity:**
85+
86+
* Time complexity : O(n).
87+
* Space complexity : O(n).

101-200/118. Pascal's Triangle.md

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# 118. Pascal's Triangle
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Array.
5+
- Similar Questions: Pascal's Triangle II.
6+
7+
## Problem
8+
9+
Given a non-negative integer **numRows**, generate the first **numRows** of Pascal's triangle.
10+
11+
![](https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif)
12+
13+
In Pascal's triangle, each number is the sum of the two numbers directly above it.
14+
15+
**Example:**
16+
17+
```
18+
Input: 5
19+
Output:
20+
[
21+
[1],
22+
[1,1],
23+
[1,2,1],
24+
[1,3,3,1],
25+
[1,4,6,4,1]
26+
]
27+
```
28+
29+
## Solution
30+
31+
```javascript
32+
/**
33+
* @param {number} numRows
34+
* @return {number[][]}
35+
*/
36+
var generate = function(numRows) {
37+
var i = 0;
38+
var j = 0;
39+
var res = [];
40+
for (i = 0; i < numRows; i++) {
41+
res.push(Array(i + 1));
42+
for (j = 0; j <= i; j++) {
43+
if (j === 0 || j === i) {
44+
res[i][j] = 1;
45+
} else {
46+
res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
47+
}
48+
}
49+
}
50+
return res;
51+
};
52+
```
53+
54+
**Explain:**
55+
56+
nope.
57+
58+
**Complexity:**
59+
60+
* Time complexity : O(n^2).
61+
* Space complexity : O(n^2).

0 commit comments

Comments
 (0)