Skip to content

Commit 0e2501f

Browse files
committed
feat(algorithms): update 27 and add 67 && 189 && 209 && 485
1 parent 66d7354 commit 0e2501f

File tree

14 files changed

+336
-29
lines changed

14 files changed

+336
-29
lines changed

algorithms/0027.remove-element/index.jest.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,39 @@ import { removeElement } from '.';
33
let a: number[] = [];
44
let filter = 1;
55
let len = removeElement(a, filter);
6-
console.info('-->', a, `filter ${filter}, ant len is ${len}\n`);
6+
console.info('-->', a, `filter ${filter}, ant len is`, len, '\n');
77

88
a = [0];
99
filter = 1;
1010
len = removeElement(a, filter);
11-
console.info('-->', a, `filter ${filter}, ant len is ${len}\n`);
11+
console.info('-->', a, `filter ${filter}, ant len is`, len, '\n');
1212

1313
a = [1];
1414
filter = 1;
1515
len = removeElement(a, filter);
16-
console.info('-->', a, `filter ${filter}, ant len is ${len}\n`);
16+
console.info('-->', a, `filter ${filter}, ant len is`, len, '\n');
17+
18+
a = [3, 2, 2, 3];
19+
filter = 3;
20+
len = removeElement(a, filter);
21+
console.info('-->', a, `filter ${filter}, ant len is`, len, '\n');
22+
23+
a = [0, 1, 2, 2, 3, 0, 4, 2];
24+
filter = 2;
25+
len = removeElement(a, filter);
26+
console.info('-->', a, `filter ${filter}, ant len is`, len, '\n');
1727

1828
a = [1, 1, 1, 1, 1, 1];
1929
filter = 1;
2030
len = removeElement(a, filter);
21-
console.info('-->', a, `filter ${filter}, ant len is ${len}\n`);
31+
console.info('-->', a, `filter ${filter}, ant len is`, len, '\n');
2232

2333
a = [1, 1, 1, 1, 1, 1, 1];
2434
filter = 2;
2535
len = removeElement(a, filter);
26-
console.info('-->', a, `filter ${filter}, ant len is ${len}\n`);
36+
console.info('-->', a, `filter ${filter}, ant len is`, len, '\n');
2737

2838
a = [1, 2, 32, 4, 5, 6, 7, 8, 9];
2939
filter = 32;
3040
len = removeElement(a, filter);
31-
console.info('-->', a, `filter ${filter}, ant len is ${len}\n`);
41+
console.info('-->', a, `filter ${filter}, ant len is`, len, '\n');
Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { S_IFBLK } from 'constants';
2-
31
/**
42
* 逻辑没有考虑完备,导致很多测试用力无法通过
53
* @state Error
@@ -22,30 +20,18 @@ export const removeElement1 = (nums: number[], val: number): number => {
2220
};
2321

2422
/**
23+
* 尾部判定
2524
* @state Accepted
26-
* @runtime
27-
* @memory
25+
* @runtime 112ms > 28.81%
26+
* @memory 33.8MB < 30.90%
2827
*/
2928
export const removeElement = (nums: number[], val: number): number => {
30-
// 伪代码
31-
// head === val && tail === val; while(p >= 0 && p !== val) replace;i++;p--;
32-
// head === val && tail !== val; replace;i++;p--;
33-
// head !== val && tail === val; i++;p--;
34-
// head !== val && tail !== val; i++;
35-
36-
let p: number = nums.length - 1;
37-
for (let i: number = 0; i < nums.length && p >= 0; i++, i = nums.length) {
38-
console.info(`::for::---> i:${i}, p:${p}`);
39-
if (nums[i] === val) {
40-
while (p >= 0 && nums[p--] === val) console.info(`::while::--->${p}`);
41-
if (p < 0) break;
42-
nums[i] = nums[i] ^ nums[p];
43-
nums[p] = nums[i] ^ nums[p];
44-
nums[i] = nums[i] ^ nums[p];
45-
nums.length = p; // remove the filter element;change numbers[] length
46-
} else if (nums[p] !== val) continue;
47-
p--;
29+
for (let p: number = 0; p < nums.length; p++) {
30+
while (nums[nums.length - 1] === val) nums.length--;
31+
if (p >= nums.length - 1) break;
32+
nums[p] = nums[p] ^ nums[nums.length - 1];
33+
nums[nums.length - 1] = nums[nums.length - 1] ^ nums[p];
34+
nums[p] = nums[p] ^ nums[nums.length - 1];
4835
}
49-
console.info(`::$::---->p:${p},nums:`, nums);
5036
return nums.length;
5137
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# 67. Add Binary
2+
3+
Given two binary strings, return their sum (also a binary string).
4+
5+
The input strings are both **non-empty** and contains only characters 1 or 0.
6+
7+
## Example
8+
9+
```bash
10+
Input: a = "11", b = "1"
11+
Output: "100"
12+
```
13+
14+
```bash
15+
Input: a = "1010", b = "1011"
16+
Output: "10101"
17+
```
18+
19+
## Related Tips
20+
21+
- Math
22+
- String
23+
24+
## Submissions
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { addBinary2 } from '.';
2+
3+
let result: string;
4+
let answer: string;
5+
6+
// result = addBinary2('11', '1');
7+
// answer = '100';
8+
// console.info('--->', result, '100', result === answer, '\n');
9+
10+
// result = addBinary2('1010', '1011');
11+
// answer = '10101';
12+
// console.info('--->', result, '10101', result === answer, '\n');
13+
14+
// result = addBinary2('11111', '11111');
15+
// answer = '100000';
16+
// console.info('--->', result, '100000', result === answer, '\n');
17+
18+
// result = addBinary2('11111', '0');
19+
// answer = '11111';
20+
// console.info('--->', result, '11111', result === answer, '\n');
21+
22+
// result = addBinary2('0', '11111');
23+
// answer = '11111';
24+
// console.info('--->', result, '11111', result === answer, '\n');
25+
26+
// result = addBinary2('11110', '1');
27+
// answer = '11111';
28+
// console.info('--->', result, '11111', result === answer, '\n');
29+
30+
result = addBinary2(
31+
'10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101',
32+
'110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011'
33+
);
34+
answer =
35+
'110111101100010011000101110110100000011101000101011001000011011000001100011110011010010011000000000';
36+
console.info('--->', result, result === answer, '\n');
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 原生方法
3+
* 无脑转换类型,忽略了 Number.MAX_SAFE_INTEGER 的损失精度的情况
4+
* @param {string} a
5+
* @param {string} b
6+
* @return {string}
7+
* @state Error
8+
* @case '10100000100100110110010000010101111011011001101110111111111101000000101111001110001111100001101'
9+
* "110101001011101110001111100110001010100001101011101010000011011011001011101111001100000011011110011"
10+
*/
11+
export const addBinary = (a: string, b: string): string => {
12+
return Number(parseInt(a, 2) + parseInt(b, 2)).toString(2);
13+
};
14+
15+
/**
16+
* 逐位计算
17+
* @param {string} a
18+
* @param {string} b
19+
* @return {string}
20+
* @state Accepted
21+
*/
22+
export const addBinary2 = (a: string, b: string): string => {
23+
const result: number[] = [];
24+
let fix: number = 0;
25+
for (let p = a.split(''), n = b.split(''); p.length > 0 || n.length > 0; ) {
26+
let sum = Number(p.pop() || '0') + Number(n.pop() || '0') + fix;
27+
if (sum <= 1) {
28+
fix = 0;
29+
}
30+
if (sum === 2) {
31+
fix = 1;
32+
sum = 0;
33+
} else if (sum === 3) {
34+
fix = 1;
35+
sum = 1;
36+
}
37+
result.unshift(sum);
38+
}
39+
40+
return (fix === 1 ? '1' : '') + result.join('');
41+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 189 Retate Array
2+
3+
Given an array, rotate the array to the right by k steps, where k is non-negative.
4+
5+
## Example
6+
7+
```bash
8+
Input: [1,2,3,4,5,6,7] and k = 3
9+
Output: [5,6,7,1,2,3,4]
10+
Explanation:
11+
rotate 1 steps to the right: [7,1,2,3,4,5,6]
12+
rotate 2 steps to the right: [6,7,1,2,3,4,5]
13+
rotate 3 steps to the right: [5,6,7,1,2,3,4]
14+
```
15+
16+
```bash
17+
Input: [-1,-100,3,99] and k = 2
18+
Output: [3,99,-1,-100]
19+
Explanation:
20+
rotate 1 steps to the right: [99,-1,-100,3]
21+
rotate 2 steps to the right: [3,99,-1,-100]
22+
```
23+
24+
## Follow up
25+
26+
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
27+
28+
## Note
29+
30+
Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
31+
Could you do it in-place with O(1) extra space?
32+
33+
## Submissions
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { rotate } from '.';
2+
3+
let nums: number[] = [1];
4+
let k: number = 5;
5+
console.info('-->', [1]);
6+
console.info('-->', rotate(nums, k), '\n');
7+
8+
nums = [1, 2, 3, 4, 5];
9+
k = 0;
10+
console.info('-->', [1, 2, 3, 4, 5]);
11+
console.info('-->', rotate(nums, k), '\n');
12+
13+
nums = [1, 2, 3, 4, 5];
14+
k = 2;
15+
console.info('-->', [4, 5, 1, 2, 3]);
16+
console.info('-->', rotate(nums, k), '\n');
17+
18+
nums = [1, 2, 3, 4, 5];
19+
k = 4;
20+
console.info('-->', [2, 3, 4, 5, 1]);
21+
console.info('-->', rotate(nums, k), '\n');
22+
23+
nums = [1, 2, 3, 4, 5];
24+
k = 5;
25+
console.info('-->', [1, 2, 3, 4, 5]);
26+
console.info('-->', rotate(nums, k), '\n');
27+
28+
nums = [1, 2, 3, 4, 5];
29+
k = 7;
30+
console.info('-->', [4, 5, 1, 2, 3]);
31+
console.info('-->', rotate(nums, k), '\n');
32+
33+
nums = [1, 2, 3, 4, 5];
34+
k = 10;
35+
console.info('-->', [1, 2, 3, 4, 5]);
36+
console.info('-->', rotate(nums, k), '\n');
37+
38+
nums = [1, 2, 3, 4, 5];
39+
k = 18;
40+
console.info('-->', [3, 4, 5, 1, 2]);
41+
console.info('-->', rotate(nums, k), '\n');
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} k
4+
* @return {void} Do not return anything, modify nums in-place instead.
5+
*/
6+
export const rotate = (nums: number[], k: number): void => {
7+
if (k === 0 || nums.length === 0) return;
8+
let ret: number = k % nums.length;
9+
if (ret === 0) return;
10+
for (let i = 0; i < ret; i++) {}
11+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 209. Minimum Size Subarray Sum
2+
3+
Given an array of n positive integers and a positive integer s, find the minimal length of a **contiguous** subarray of which the sum ≥ s. If there isn't one, return 0 instead.
4+
5+
## Example
6+
7+
```bash
8+
Input: s = 7, nums = [2,3,1,2,4,3]
9+
Output: 2
10+
Explanation: the subarray [4,3] has the minimal length under the problem constraint.
11+
```
12+
13+
## Follow up
14+
15+
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
16+
17+
## Related Tips
18+
19+
- Array
20+
- Two Pointer
21+
- Binary Search
22+
23+
## Submissions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { minSubArrayLen } from '.';
2+
3+
console.info(1, [1]);
4+
console.info('-->>', minSubArrayLen(1, [1]), '\n');
5+
6+
console.info(2, [1]);
7+
console.info('-->>', minSubArrayLen(2, [1]), '\n');
8+
9+
console.info(4, [1, 1, 1, 1, 2, 2]);
10+
console.info('-->>', minSubArrayLen(4, [1, 1, 1, 1]), '\n');
11+
12+
console.info(7, [1, 1, 1, 1]);
13+
console.info('-->>', minSubArrayLen(7, [1, 1, 1, 1]), '\n');
14+
15+
console.info(7, [2, 3, 1, 2, 5, 3]);
16+
console.info('-->>', minSubArrayLen(7, [2, 3, 1, 2, 5, 3]), '\n');
17+
18+
console.info(9, [9, 3, 1, 2, 8, 3]);
19+
console.info('-->>', minSubArrayLen(9, [9, 3, 1, 2, 8, 3]), '\n');
20+
21+
console.info(9, [1, 3, 1, 2, 8, 3, 9]);
22+
console.info('-->>', minSubArrayLen(9, [1, 3, 1, 2, 8, 3, 9]), '\n');
23+
24+
console.info(2, [1, 4, 5, 6, 7, 8, 9]);
25+
console.info('-->>', minSubArrayLen(2, [1, 4, 5, 6, 7, 8, 9]), '\n');

0 commit comments

Comments
 (0)