Skip to content

Commit 5980df4

Browse files
committed
Merge branch 'gh-pages' of https://github.com/guocaoyi/leetcode-cn
2 parents f99233b + 1ddc7d4 commit 5980df4

File tree

32 files changed

+434
-475
lines changed

32 files changed

+434
-475
lines changed

Algorithms/0001.Two Sum/index.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

Algorithms/0001.Two Sum/index.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* 方案一:两次列表遍历
3+
* @param {number[]} nums
4+
* @param {number} target
5+
* @return {number[]}
6+
*/
7+
export function onTwiceBubble(
8+
nums: Array<number>,
9+
target: number
10+
): Array<number> {
11+
for (let i = 0; i < nums.length; i++) {
12+
for (let j = i + 1; j < nums.length; j++) {
13+
if (nums[i] + nums[j] == target) {
14+
return [i, j];
15+
}
16+
}
17+
}
18+
}
19+
20+
/**
21+
* 方案二:两次哈希遍历
22+
* @param {number[]} nums
23+
* @param {number} target
24+
* @return {number[]}
25+
*/
26+
export function onTwiceIteratorHash(
27+
nums: Array<number>,
28+
target: number
29+
): Array<number> {
30+
const map: any = {};
31+
const length = nums.length;
32+
// 使用Array.forEach在性能上会有点损耗(测试用例:61ms到59ms)
33+
for (let i = 0; i < length; i++) {
34+
map[nums[i]] = i;
35+
}
36+
for (let i = 0; i < length; i++) {
37+
const x = target - nums[i];
38+
if (x in map && map[x] != i) {
39+
return [i, map[x]];
40+
}
41+
}
42+
}
43+
44+
/**
45+
* 方案三:一次哈希遍历
46+
* @param {number[]} nums
47+
* @param {number} target
48+
* @return {number[]}
49+
*/
50+
export function onIteratorHash(
51+
nums: Array<number>,
52+
target: number
53+
): Array<number> {
54+
const map: any = {};
55+
const length = nums.length;
56+
for (let i = 0; i < length; i++) {
57+
const x = target - nums[i];
58+
if (x in map && map[x] != i) {
59+
return [map[x], i];
60+
}
61+
map[nums[i]] = i;
62+
}
63+
}

Algorithms/0003.Longest Substring Without Repeating Characters/README.md

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
* 987test case,132ms
5+
*/
6+
const slution1 = (s: string): number => {
7+
let maxSub = "",
8+
currentSub = "";
9+
const arr = s.split("");
10+
arr.forEach((s: string) => {
11+
if (currentSub.includes(s)) {
12+
// 存在
13+
if (currentSub.length >= maxSub.length) {
14+
maxSub = currentSub;
15+
}
16+
let [lStr, rStr] = currentSub.split(s);
17+
currentSub = rStr || "";
18+
currentSub += s;
19+
} else {
20+
// 不存在
21+
currentSub += s;
22+
if (currentSub.length >= maxSub.length) {
23+
maxSub = currentSub;
24+
}
25+
}
26+
});
27+
return maxSub.length;
28+
};
29+
30+
/**
31+
* 987test case,184ms
32+
*/
33+
let slution2 = (s: string): number => {
34+
const obj = {
35+
subStr: "",
36+
maxLen: 0
37+
};
38+
for (let i: number = 0; i < s.length; i++) {
39+
let strArray: string[] = obj.subStr.split(s[i]);
40+
obj.subStr = strArray[strArray.length - 1] + s[i];
41+
obj.maxLen = Math.max(obj.maxLen, obj.subStr.length);
42+
}
43+
return obj.maxLen;
44+
};

Algorithms/0003.Longest Substring Without Repeating Characters/longestSubstringWithoutRepeatingCharacters.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

Algorithms/0004.Median of Two Sorted Arrays/medianOfTwoSortedArrays.js

Lines changed: 0 additions & 27 deletions
This file was deleted.
Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
"use strict"
1+
# 将字符串 "PAYPALISHIRING" 以 Z 字形排列成给定的行数:
22

3-
/**
4-
* 将字符串 "PAYPALISHIRING" 以Z字形排列成给定的行数:
5-
6-
P A H N
3+
P A H N
74
A P L S I I G
8-
Y I R
5+
Y I R
96
之后从左往右,逐行读取字符:"PAHNAPLSIIGYIR"
107

118
实现一个将字符串进行指定行数变换的函数:
@@ -21,10 +18,7 @@ string convert(string s, int numRows);
2118
输出: "PINALSIGYAHRPI"
2219
解释:
2320

24-
P I N
25-
A L S I G
26-
Y A H R
27-
P I
28-
*/
29-
30-
21+
P I N
22+
A L S I G
23+
Y A H R
24+
P I
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/**
2+
*
3+
*/

Algorithms/0014.Longest Common Prefix/READMD.md renamed to Algorithms/0014.Longest Common Prefix/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ var longestCommonPrefix = function(strs) {
9494

9595
### III: 单次遍历
9696

97-
使用`strs[0]`作为初始前缀串,逐一遍历`strs[]`元素进行比较,如`String.indexOf === -1`则自减长度 1,直至为 0 成立后继续访问下面的元素
97+
使用 `strs[0]` 作为初始前缀串,逐一遍历 `strs[]` 元素进行比较,如 `String.indexOf !== 0` 则自减长度 1,直至成立后继续访问后面的元素
9898

9999
- language: javascript
100100
- status: Accepted

Algorithms/0014.Longest Common Prefix/index.js

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)