Skip to content

Commit 1bb2503

Browse files
committed
update
1 parent c18a4a7 commit 1bb2503

6 files changed

+135
-2
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,16 @@
2323
##### [Contains-Duplicate](./leetcode/Contains-Duplicate.ts)
2424
##### [Rotate-Array](./leetcode/Rotate-Array.ts)
2525
##### [Single-Number](./leetcode/Single-Number.ts)
26-
##### [Intersection-of-Two-Arrays-II](./leetcode/Intersection-of-Two-Arrays-II.ts)
26+
##### **[Intersection-of-Two-Arrays-II](./leetcode/Intersection-of-Two-Arrays-II.ts)**
2727
##### [Plus-One](./leetcode/Plus-One.ts)
2828
##### [Move-Zeroes](./leetcode/Move-Zeroes.ts)
29+
##### [Two-Sum](./leetcode/Two-Sum.ts)
30+
##### [Find-the-Duplicate-Number](./leetcode/Find-the-Duplicate-Number.ts) ??
31+
##### [Find-Pivot-Index](./leetcode/Find-Pivot-Index.ts)
32+
33+
## 字符串
34+
##### [Shortest-Distance-to-a-Character](./leetcode/Shortest-Distance-to-a-Character.ts)
35+
2936

3037
## 双指针
3138
##### [remove-duplicates-from-sorted-array](./leetcode/Remove-Duplicates-from-Sorted-Array.ts)

leetcode/Find-Pivot-Index.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* https://leetcode.com/problems/find-pivot-index/description/
3+
*/
4+
5+
/**
6+
* 先求出总和,然后遍历累加的左边的和,用总和减去当前值和左边的和就右边的和,比较他们的值。
7+
*/
8+
const pivotIndex = (nums: number[]): number => {
9+
const totalSum = nums.reduce((a, b) => a + b)
10+
let leftSum = 0
11+
for(let i = 0; i < nums.length; i++) {
12+
if(leftSum === totalSum - nums[i]) return i
13+
leftSum += nums[i]
14+
}
15+
return -1
16+
};

leetcode/Find-the-Duplicate-Number.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* https://leetcode.com/problems/find-the-duplicate-number/description/
3+
*/
4+
5+
/**
6+
* 暴力查找
7+
*/
8+
// const findDuplicate = (nums: number[]): number => {
9+
// for (let i = 0; i < nums.length; i++) {
10+
// for (let j = i + 1; j < nums.length; j++) {
11+
// if (nums[j] === nums[i]) return nums[j];
12+
// }
13+
// }
14+
// return Number.MIN_SAFE_INTEGER;
15+
// };
16+
17+
/**
18+
* 排序,略
19+
*/
20+
21+
/**
22+
* hash表,略
23+
*/
24+
25+
/**
26+
* https://leetcode.com/problems/find-the-duplicate-number/solution/?page=2
27+
* Floyd's Tortoise and Hare (Cycle Detection)
28+
*/
29+
30+
//@TODO

leetcode/Intersection-of-Two-Arrays-II.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* 定义一个空数组,然后遍历 nums2, 如果 nums2 中的值在哈希表中存在,且数量大于 0 ,则将这个 key 推入 result 中。数量 -1。
88
*/
99
// const intersect = (nums1: number[], nums2: number[]): number[] => {
10-
// const intersectMap = new Map();
10+
// const intersectMap = {};
1111
// for (let i = 0; i < nums1.length; i++) {
1212
// intersectMap[nums1[i]] = (intersectMap[nums1[i]] || 0) + 1;
1313
// }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* https://leetcode.com/problems/shortest-distance-to-a-character/description/
3+
*/
4+
5+
/**
6+
* 遍历得到目标c的位置。
7+
* 在遍历,求出每一点到c的距离中的最小值。
8+
*/
9+
// const shortestToChar = (S: string, C: string) => {
10+
// const result = [];
11+
// const targetIndex = [];
12+
// for (let i = 0; i < S.length; i++) {
13+
// if (S[i] === C) targetIndex.push(i);
14+
// }
15+
// for (let j = 0; j < S.length; j++) {
16+
// const minD = targetIndex.reduce((result, index) => {
17+
// result = Math.min(Math.abs(index - j), result);
18+
// return result;
19+
// }, Number.MAX_SAFE_INTEGER);
20+
// result[j] = minD;
21+
// }
22+
// return result;
23+
// };
24+
25+
/**
26+
* “loveleetcode”
27+
* 从左到右遍历一遍,得到该位置到最近的左边的 c 的距离。[0, 0, 0, 3, 1, 5, 6, 1, 2, 3, 4, 11]
28+
* 再从右到左遍历一遍,得到该位置到最近的右边的 c 的距离, 求左边距离和右边距离中的最小值
29+
*/
30+
const shortestToChar = (S: string, C: string) => {
31+
const result = [];
32+
33+
let leftPosition = Number.MIN_SAFE_INTEGER;
34+
for (let i = 0; i < S.length; i++) {
35+
if (S[i] === C) leftPosition = i;
36+
result[i] = i - leftPosition;
37+
}
38+
39+
let rightPosition = Number.MAX_SAFE_INTEGER;
40+
for (let i = S.length - 1; i >= 0; i--) {
41+
if (S[i] === C) rightPosition = i;
42+
result[i] = Math.min(rightPosition - i, result[i]);
43+
}
44+
45+
return result;
46+
};

leetcode/Two-Sum.ts

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* https://leetcode.com/problems/two-sum/description/
3+
*/
4+
5+
/**
6+
* 使用 map,然后找到 tartget - nums[i] 是否存在
7+
*/
8+
const twoSum = function(nums: number[], target: number): number[] {
9+
const numsMap = new Map();
10+
for (let i = 0; i < nums.length; i++) {
11+
if (nums[i] > target) continue
12+
if (numsMap.has(target - nums[i])) return [numsMap.get(target - nums[i]), i];
13+
numsMap.set(nums[i], i);
14+
}
15+
return [-1, -1];
16+
};
17+
18+
/**
19+
* 如果数组是已排序的,双指针向中间靠拢
20+
*/
21+
22+
// const twoSum = function(nums: number[], target: number): number[] {
23+
// let [i, j] = [0, nums.length - 1];
24+
// while (j > i) {
25+
// const sum = nums[i] + nums[j];
26+
// if (sum > target) {
27+
// j--;
28+
// } else if (sum < target) {
29+
// i++;
30+
// } else {
31+
// return [i, j];
32+
// }
33+
// }
34+
// };

0 commit comments

Comments
 (0)