Skip to content

Commit 305661c

Browse files
committed
update
1 parent 7def024 commit 305661c

File tree

12 files changed

+283
-149
lines changed

12 files changed

+283
-149
lines changed

CATEGORY.md

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,8 @@ https://github.com/garciparedes
66

77
https://garciparedes.me/#headers
88

9-
- Array +12
10-
11-
- [ ] 15
12-
- [ ] 18
13-
- [ ] 39
14-
- [ ] 40
15-
- [ ] 45
16-
- [ ] 55
17-
- [ ] 78
18-
- [ ] 88
19-
- [ ] 90
20-
219
- Backtracking
2210

23-
- [ ] 17
2411
- [ ] 22
2512
- [ ] 39
2613
- [ ] 40
@@ -36,6 +23,7 @@ https://garciparedes.me/#headers
3623
- [ ] 306
3724
- [ ] 357
3825
- [ ] 872
26+
- [ ] 17
3927

4028
- Breadth First Search +5
4129

@@ -57,7 +45,6 @@ https://garciparedes.me/#headers
5745

5846
- Dynamic Programming +7
5947

60-
- [ ] 376
6148
- [ ] 1143
6249
- [ ] 5
6350
- [ ] 91
@@ -69,6 +56,7 @@ https://garciparedes.me/#headers
6956
- [ ] 322
7057
- [ ] 343
7158
- [ ] 357
59+
- [ ] 376
7260

7361
- Greedy
7462

@@ -129,3 +117,13 @@ https://garciparedes.me/#headers
129117

130118
- [ ] 3
131119
- [ ] 16
120+
121+
- Array +12
122+
123+
- [ ] 18
124+
- [ ] 39
125+
- [ ] 40
126+
- [ ] 45
127+
- [ ] 55
128+
- [ ] 78
129+
- [ ] 90
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {number}
5+
*/
6+
7+
var longestOnes = function (nums, k) {
8+
let [left, right, res, zeroCount] = [0, 0, 0, 0];
9+
10+
while (right < nums.length) {
11+
if (nums[right] === 0) zeroCount++;
12+
if (zeroCount <= k) {
13+
res = Math.max(res, right - left + 1);
14+
}
15+
16+
while (zeroCount > k) {
17+
if (nums[left] === 0) zeroCount--;
18+
left++;
19+
}
20+
21+
right++;
22+
}
23+
24+
return res;
25+
};
Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
// HELP:
2-
3-
/**
4-
* 滑动窗口
5-
* 时间:O(N), 88ms
6-
*/
7-
var longestOnes = function(A, K) {
8-
let zeroCount = 0;
9-
let [left, right] = [0, 0];
10-
let res = 0;
11-
12-
while (right < A.length) {
13-
const newNumber = A[right++];
14-
if (newNumber === 0) ++zeroCount;
15-
if (zeroCount <= K) {
16-
// 能替换所有0
17-
res = Math.max(res, right - left);
18-
}
19-
while (zeroCount > K) {
20-
// 不能替换的情况:删除字符
21-
const oldNumber = A[left++];
22-
if (oldNumber === 0) --zeroCount;
23-
}
24-
}
25-
26-
return res;
27-
};
1+
// HELP:
2+
3+
/**
4+
* 滑动窗口
5+
* 时间:O(N), 88ms
6+
*/
7+
var longestOnes = function (nums, k) {
8+
let [left, right, zeroCount, res] = [0, 0, 0, 0];
9+
10+
while (right < nums.length) {
11+
if (nums[right] === 0) zeroCount++;
12+
13+
if (zeroCount <= k) {
14+
// 能替换所有0
15+
res = Math.max(res, right - left + 1);
16+
}
17+
while (zeroCount > k) {
18+
// 不能替换的情况:删除字符
19+
if (nums[left] === 0) zeroCount--;
20+
left++;
21+
}
22+
23+
right++;
24+
}
25+
26+
return res;
27+
};

src/15-3Sum/20210729.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
function threeSum(nums) {
2+
if (nums.length < 3) return [];
3+
nums.sort((x, y) => x - y);
4+
const res = [];
5+
6+
for (let i = 0; i < nums.length - 2; i++) {
7+
if (nums[i] === nums[i - 1]) continue;
8+
let left = i + 1;
9+
let right = nums.length - 1;
10+
while (left < right) {
11+
if (nums[i] + nums[left] + nums[right] === 0) {
12+
res.push([nums[i], nums[left], nums[right]]);
13+
left++;
14+
right--;
15+
while (nums[left] === nums[left - 1] && left < right) {
16+
left++;
17+
}
18+
while (nums[right] === nums[right + 1] && left < right) {
19+
right--;
20+
}
21+
continue;
22+
}
23+
24+
if (nums[i] + nums[left] + nums[right] < 0) {
25+
left++;
26+
} else {
27+
right--;
28+
}
29+
}
30+
}
31+
32+
return res;
33+
}
34+
35+
threeSum([-1, 0, 1, 2, -1, -4]);
Lines changed: 23 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,23 @@
1-
/**
2-
* @param {string} digits
3-
* @return {string[]}
4-
*/
5-
var letterCombinations = function(digits) {
6-
const hash = [
7-
'',
8-
'',
9-
'abc',
10-
'def',
11-
'ghi',
12-
'jkl',
13-
'mno',
14-
'pqrs',
15-
'tuv',
16-
'wxyz',
17-
];
18-
19-
const arr = digits.split('').map(n => hash[n]);
20-
let res = [];
21-
arr.length > 0 && backtrack(arr, []);
22-
23-
return res;
24-
25-
function backtrack(rest, temp) {
26-
if (rest.length === 0) {
27-
res.push(temp);
28-
return;
29-
}
30-
31-
const current = rest[0];
32-
for (let char of current) {
33-
backtrack(rest.slice(1), temp + char);
34-
}
35-
}
36-
};
37-
38-
console.log(letterCombinations(''));
1+
/**
2+
* @param {string} digits
3+
* @return {string[]}
4+
*/
5+
var letterCombinations = function (digits) {
6+
if (digits === "") return [];
7+
const hash = ["", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"].map((x) => x.split(""));
8+
const res = [];
9+
dfs("", 0);
10+
return res;
11+
12+
function dfs(temp, index) {
13+
if (index === digits.length) {
14+
return res.push(temp);
15+
}
16+
17+
for (let char of hash[digits[index]]) {
18+
dfs(temp + char, index + 1);
19+
}
20+
}
21+
};
22+
23+
console.log(letterCombinations(""));

src/18-4Sum/20210729.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
function fourSum(nums, target) {
2+
if (nums.length < 4) return [];
3+
4+
nums.sort((x, y) => x - y);
5+
const res = [];
6+
7+
for (let i = 0; i < nums.length - 3; i++) {
8+
if (nums[i] === nums[i - 1]) continue;
9+
for (let j = i + 1; j < nums.length - 2; j++) {
10+
if (nums[j] === nums[j - 1] && j > i + 1) continue;
11+
let l = j + 1;
12+
let r = nums.length - 1;
13+
while (l < r) {
14+
if (nums[i] + nums[j] + nums[l] + nums[r] === target) {
15+
res.push([nums[i], nums[j], nums[l], nums[r]]);
16+
l++;
17+
r--;
18+
while (nums[l] === nums[l - 1] && l < r) {
19+
l++;
20+
}
21+
while (nums[r] === nums[r + 1] && l < r) {
22+
r--;
23+
}
24+
} else if (nums[i] + nums[j] + nums[l] + nums[r] < target) {
25+
l++;
26+
} else {
27+
r--;
28+
}
29+
}
30+
}
31+
}
32+
33+
return res;
34+
}
35+
36+
console.log(fourSum([-2, -1, -1, 1, 1, 2, 2], 0));

src/22-generate-parentheses/index.js

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,24 @@
1-
/**
2-
* @param {number} n
3-
* @return {string[]}
4-
*/
5-
var generateParenthesis = function(n) {
6-
let res = ['()'];
7-
8-
let i = 2;
9-
10-
while (i <= n) {
11-
let temp = [];
12-
for (let j = 0; j < res.length; j++) {
13-
const curr = res[j];
14-
let k = 0;
15-
while (k <= curr.length) {
16-
const withLeft = [...curr];
17-
withLeft.splice(k, 0, '(');
18-
19-
let m = k + 1;
20-
while (m <= curr.length + 2) {
21-
const withRight = [...withLeft];
22-
withRight.splice(m, 0, ')');
23-
m += 2;
24-
temp.push(withRight.join(''));
25-
}
26-
27-
k += 2;
28-
}
29-
}
30-
res = Array.from(new Set(temp));
31-
i++;
32-
}
33-
34-
return res;
35-
};
36-
37-
console.log(generateParenthesis(3));
1+
/**
2+
* @param {number} n
3+
* @return {string[]}
4+
*/
5+
var generateParenthesis = function (n) {
6+
let res = [];
7+
let temp = "";
8+
dfs(temp, 0, 0);
9+
return res;
10+
11+
function dfs(temp, left, right) {
12+
if (left < right || left > n || right > n) {
13+
return;
14+
}
15+
if (left === n && right === n) {
16+
res.push(temp);
17+
return;
18+
}
19+
dfs(temp + "(", left + 1, right);
20+
dfs(temp + ")", left, right + 1);
21+
}
22+
};
23+
24+
console.log(generateParenthesis(3));

src/22-generate-parentheses/pro.js

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
// HELP:
2-
/**
3-
* @param {number} n
4-
* @return {string[]}
5-
*/
6-
var generateParenthesis = function(n) {
7-
let res = [];
8-
9-
let temp = '';
10-
let left = 0;
11-
let right = 0;
12-
13-
backtrack(res, temp, left, right, n);
14-
return res;
15-
16-
function backtrack(res, temp, left, right, n) {
17-
// 减枝
18-
if (left < right || left > n || right > n) {
19-
return;
20-
}
21-
// 命中
22-
if (left == n && right == n) {
23-
res.push(temp);
24-
return;
25-
}
26-
// 递归
27-
backtrack(res, temp + '(', left + 1, right, n);
28-
backtrack(res, temp + ')', left, right + 1, n);
29-
}
30-
};
1+
// HELP:
2+
/**
3+
* @param {number} n
4+
* @return {string[]}
5+
*/
6+
var generateParenthesis = function (n) {
7+
let res = [];
8+
9+
let temp = "";
10+
let left = 0;
11+
let right = 0;
12+
13+
backtrack(temp, left, right, n);
14+
return res;
15+
16+
function backtrack(temp, left, right, n) {
17+
// 减枝
18+
if (left < right || left > n || right > n) {
19+
return;
20+
}
21+
// 命中
22+
if (left == n && right == n) {
23+
res.push(temp);
24+
return;
25+
}
26+
// 递归
27+
backtrack(temp + "(", left + 1, right, n);
28+
backtrack(temp + ")", left, right + 1, n);
29+
}
30+
};

0 commit comments

Comments
 (0)