Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 85ebcf0

Browse files
committed
feat(doc):add new style doc
1 parent fa1c416 commit 85ebcf0

File tree

38 files changed

+1898
-231
lines changed

38 files changed

+1898
-231
lines changed

‎README.md

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,18 @@ LeetCode problems' submissions & top randed with TypeScript.
2626

2727
## Algorithms
2828

29-
| \# | Title | Topics | Difficulty |
30-
| :------------------------------------------------------------------------------- | :--------------------------------------------- | :----- | :----------------------------------------: |
31-
| [0001](algorithms/0001.two-sum/README.md) | two-sum | String | <span class='dif-tag easy'>Easy</span> |
32-
| [0003](algorithms/0003.longest-substring-without-repeating-characters/README.md) | longest-substring-without-repeating-characters | String | <span class='dif-tag easy'>Easy</span> |
33-
| [0004](algorithms/0004.median-of-two-sorted-arrays/README.md) | median-of-two-sorted-arrays | String | <span class='dif-tag hard'>Hard</span> |
34-
| [0014](algorithms/0014.longest-common-prefix/README.md) | longest-common-prefix | String | <span class='dif-tag easy'>Easy</span> |
35-
| [0017](algorithms/0017.letter-combinations-of-a-phone-number/README.md) | letter-combinations-of-a-phone-number | String | <span class='dif-tag easy'>Easy</span> |
36-
| [0050](algorithms/0050.powx-n) | powx-n | String | <span class='dif-tag easy'>Easy</span> |
37-
| [0074](algorithms/0074.search-a-2d-matrix/README.md) | search-a-2d-matrix | String | <span class='dif-tag easy'>Easy</span> |
38-
| [0136](algorithms/0136.single-number/README.md) | single-number | String | <span class='dif-tag medium'>Medium</span> |
39-
| [0169](algorithms/0169.majority-element/README.md) | majority-element | String | <span class='dif-tag easy'>Easy</span> |
40-
| [0202](algorithms/0202.happy-number/README.md) | happy-number | String | <span class='dif-tag easy'>Easy</span> |
41-
| [0240](algorithms/0240.search-a-2d-matrix-ii/README.md) | search-a-2d-matrix-ii | String | <span class='dif-tag easy'>Easy</span> |
42-
| [0344](algorithms/0344.reverse-string/README.md) | reverse-string | String | <span class='dif-tag easy'>Easy</span> |
43-
| [0724](algorithms/0724.find-pivot-index/README.md) | find-pivot-inde | String | <span class='dif-tag hard'>Hard</span> |
29+
| \# | Title | Topics | Difficulty |
30+
| :---------------------------------------------------------------------------- | :--------------------------------------------- | :------------------------------------ | :----------------------------------------: |
31+
| [1](algorithms/0001.two-sum/README.md) | two-sum | String | <span class='dif-tag easy'>Easy</span> |
32+
| [3](algorithms/0003.longest-substring-without-repeating-characters/README.md) | longest-substring-without-repeating-characters | Array,Dynamic Programming,Math,String | <span class='dif-tag easy'>Easy</span> |
33+
| [4](algorithms/0004.median-of-two-sorted-arrays/README.md) | median-of-two-sorted-arrays | String | <span class='dif-tag hard'>Hard</span> |
34+
| [14](algorithms/0014.longest-common-prefix/README.md) | longest-common-prefix | String | <span class='dif-tag easy'>Easy</span> |
35+
| [17](algorithms/0017.letter-combinations-of-a-phone-number/README.md) | letter-combinations-of-a-phone-number | String | <span class='dif-tag easy'>Easy</span> |
36+
| [50](algorithms/0050.powx-n) | powx-n | String | <span class='dif-tag easy'>Easy</span> |
37+
| [74](algorithms/0074.search-a-2d-matrix/README.md) | search-a-2d-matrix | String | <span class='dif-tag easy'>Easy</span> |
38+
| [136](algorithms/0136.single-number/README.md) | single-number | String | <span class='dif-tag medium'>Medium</span> |
39+
| [169](algorithms/0169.majority-element/README.md) | majority-element | String | <span class='dif-tag easy'>Easy</span> |
40+
| [202](algorithms/0202.happy-number/README.md) | happy-number | String | <span class='dif-tag easy'>Easy</span> |
41+
| [240](algorithms/0240.search-a-2d-matrix-ii/README.md) | search-a-2d-matrix-ii | String | <span class='dif-tag easy'>Easy</span> |
42+
| [344](algorithms/0344.reverse-string/README.md) | reverse-string | String | <span class='dif-tag easy'>Easy</span> |
43+
| [724](algorithms/0724.find-pivot-index/README.md) | find-pivot-inde | String | <span class='dif-tag hard'>Hard</span> |

‎TOPICS.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
- Array
44
- [0014.Longest Common Prefix](algorithms/0014.Longest Common Prefix/README.md)
55
- [0017.Letter Combinations of a Phone Number](algorithms/0017.Letter Combinations of a Phone Number/README.md)
6-
76
- String
87
- [0001.Two Sum](algorithms/0001.Two Sum/README.md)
98
- [0003.Longest Substring Without Repeating Characters](algorithms/0003.Longest Substring Without Repeating Characters/README.md)

‎algorithms/0001.two-sum/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Given an array of integers, return **indices** of the two numbers such that they add up to a specific target.
44

5-
You may assume that each input would have **exactly** one solution, and you may not use the same element twice.
5+
You may assume that each input would have **_exactly_** one solution, and you may not use the same element twice.
66

77
## Example
88

@@ -23,7 +23,7 @@ return [0, 1].
2323
### I: 嵌套循环遍历
2424

2525
这事最偷懒的办法,快速实现后,再考虑优化方案
26-
使用Array.forEach在性能上会有点损耗(测试用例:61ms到59ms)
26+
使用 Array.forEach 在性能上会有点损耗(测试用例:61ms 到 59ms)
2727

2828
- Time: 2018年9月13日
2929
- Status: Accepted
@@ -100,4 +100,3 @@ var twoSum = (nums: number[], target: number): number[] => {
100100
```
101101

102102
## Top Ranked
103-

‎algorithms/0001.two-sum/index.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
`
2+
1.Tow Sum
3+
4+
Given an array of integers, return **indices** of the two numbers such that they add up to a specific target.
5+
6+
You may assume that each input would have ***exactly*** one solution, and you may not use the same element twice.
7+
8+
"""
9+
Given nums = [2, 7, 11, 15], target = 9,
10+
11+
Because nums[0] + nums[1] = 2 + 7 = 9,
12+
return [0, 1].
13+
"""
14+
15+
>>> Array,Hash Table
16+
`;
17+
let twoSum: (nums: number[], target: number) => number[];
18+
119
/**
220
* 嵌套循环遍历
321
* 这事最偷懒的办法,快速实现后,再考虑优化方案
@@ -6,7 +24,7 @@
624
* @status Accepted
725
* @runtime 120ms | 40.065%
826
*/
9-
vartwoSum = (nums: number[], target: number): number[] => {
27+
twoSum = (nums: number[], target: number): number[] => {
1028
for (let i: number = 0; i < nums.length; i++) {
1129
for (let j = i + 1; j < nums.length; j++) {
1230
if (nums[i] + nums[j] == target) {
@@ -22,7 +40,7 @@ var twoSum = (nums: number[], target: number): number[] => {
2240
* @status Accepted
2341
* @runtime 80 ms | 59.00%
2442
*/
25-
vartwoSum = (nums: number[], target: number): number[] => {
43+
twoSum = (nums: number[], target: number): number[] => {
2644
const map: any = {};
2745
nums.forEach((i, k) => (map[i] = k));
2846
for (let i = 0; i < nums.length; i++) {
@@ -39,7 +57,7 @@ var twoSum = (nums: number[], target: number): number[] => {
3957
* @status Accepted
4058
* @runtime 56ms | 100.00%
4159
*/
42-
vartwoSum = (nums: number[], target: number): number[] => {
60+
twoSum = (nums: number[], target: number): number[] => {
4361
const map: any = {};
4462
const length: number = nums.length;
4563
for (let i = 0; i < length; i++) {
@@ -59,7 +77,7 @@ var twoSum = (nums: number[], target: number): number[] => {
5977
* @status Accepted
6078
* @runtime 52ms | 100%
6179
*/
62-
vartwoSum = (nums: number[], target: number): number[] => {
80+
twoSum = (nums: number[], target: number): number[] => {
6381
const map: any = {};
6482
const length = nums.length;
6583
for (let i = 0; i < length; i++) {
@@ -70,3 +88,5 @@ var twoSum = (nums: number[], target: number): number[] => {
7088
map[nums[i]] = i;
7189
}
7290
};
91+
92+
export default twoSum;

‎algorithms/0003.longest-substring-without-repeating-characters/README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,25 @@ Explanation: The answer is "wke", with the length of 3.
3232

3333
## Submissions
3434

35-
### I: **
35+
### I: \*\*
3636

3737
- Time: 2018年10月19日
3838
- Status: Output Limit Exceeded
3939

4040
```typescript
4141
var lengthOfLongestSubstring = (s: string): number => {
42-
let maxSub = "",
43-
currentSub = "";
42+
let maxSub = '',
43+
currentSub = '';
4444

45-
const arr: string[] = s.split("");
45+
const arr: string[] = s.split('');
4646
arr.forEach((s: string) => {
4747
if (currentSub.includes(s)) {
4848
// 存在
4949
if (currentSub.length >= maxSub.length) {
5050
maxSub = currentSub;
5151
}
5252
let [lStr, rStr] = currentSub.split(s);
53-
currentSub = rStr || "";
53+
currentSub = rStr || '';
5454
currentSub += s;
5555
} else {
5656
// 不存在
@@ -64,26 +64,26 @@ var lengthOfLongestSubstring = (s: string): number => {
6464
};
6565
```
6666

67-
### II: **
67+
### II: \*\*
6868

6969
- Time: 2018年10月19日
7070
- Status: Accepted
7171
- Runtime: 116ms | 55.08%
7272

7373
```typescript
7474
var lengthOfLongestSubstring = (s: string): number => {
75-
let maxSub: string = "",
76-
currentSub: string = "";
75+
let maxSub: string = '',
76+
currentSub: string = '';
7777

78-
const arr: string[] = s.split("");
78+
const arr: string[] = s.split('');
7979
arr.forEach((s: string) => {
8080
if (currentSub.includes(s)) {
8181
// 存在
8282
if (currentSub.length >= maxSub.length) {
8383
maxSub = currentSub;
8484
}
8585
let [lStr, rStr] = currentSub.split(s);
86-
currentSub = rStr || "";
86+
currentSub = rStr || '';
8787
currentSub += s;
8888
} else {
8989
// 不存在
@@ -98,4 +98,3 @@ var lengthOfLongestSubstring = (s: string): number => {
9898
```
9999

100100
## Top Ranked
101-

‎algorithms/0003.longest-substring-without-repeating-characters/index.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,37 @@
1+
`
2+
3. Longest Substring Without Repeating Characters
3+
4+
Given a string, find the length of the longest substring without repeating characters.
5+
6+
"""
7+
Input: "abcabcbb"
8+
Output: 3
9+
Explanation: The answer is "abc", with the length of 3.
10+
"""
11+
12+
"""
13+
Input: "bbbbb"
14+
Output: 1
15+
Explanation: The answer is "b", with the length of 1.
16+
"""
17+
18+
"""
19+
Input: "pwwkew"
20+
Output: 3
21+
Explanation: The answer is "wke", with the length of 3.
22+
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
23+
"""
24+
25+
>>> Hash Table,String,Sliding Window
26+
`;
27+
let lengthOfLongestSubstring: (s: string) => number;
28+
129
/**
230
* @time 2018年10月19日
331
* @status Output Limit Exceeded
4-
* @input `"unpvlhkldvfzvzwdfhojkyczxydauiioxzlkhvvmqamnakrfrhqefsddqifmqocpnoawlvjcyxpyhifbqxhxpkchuivky"`
32+
* @case `"unpvlhkldvfzvzwdfhojkyczxydauiioxzlkhvvmqamnakrfrhqefsddqifmqocpnoawlvjcyxpyhifbqxhxpkchuivky"`
533
*/
6-
varlengthOfLongestSubstring = (s: string): number => {
34+
lengthOfLongestSubstring = (s: string): number => {
735
let maxSub = '',
836
currentSub = '';
937

@@ -34,7 +62,7 @@ var lengthOfLongestSubstring = (s: string): number => {
3462
* @status Accepted
3563
* @runtime 116ms | 55.08%
3664
*/
37-
varlengthOfLongestSubstring = (s: string): number => {
65+
lengthOfLongestSubstring = (s: string): number => {
3866
let maxSub: string = '',
3967
currentSub: string = '';
4068

@@ -58,3 +86,5 @@ var lengthOfLongestSubstring = (s: string): number => {
5886
});
5987
return maxSub.length;
6088
};
89+
90+
export default lengthOfLongestSubstring;
Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,41 @@
1+
`
2+
4. Median of Two Sorted Arrays
3+
4+
There are two sorted arrays \`nums1\` and \`nums2\` of size m and n respectively.
5+
6+
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
7+
8+
You may assume \`nums1\` and \`nums2\` cannot be both empty.
9+
10+
"""
11+
nums1 = [1, 3]
12+
nums2 = [2]
13+
14+
The median is 2.0
15+
"""
16+
17+
"""
18+
nums1 = [1, 2]
19+
nums2 = [3, 4]
20+
21+
The median is (2 + 3)/2 = 2.5
22+
"""
23+
24+
>>> Array,Binary Search,Divide and Conquer
25+
`;
26+
let findMedianSortedArrays: (nums1: number[], nums2: number[]) => number;
27+
128
/**
229
* 递归
330
* @time 2019年1月4日
431
* @status Accepted
532
* @runtime 168ms | 19.48%
633
* @memory 15.8MB |
734
*/
8-
varfindMedianSortedArrays = (nums1: number[], nums2: number[]): number => {
35+
findMedianSortedArrays = (nums1: number[], nums2: number[]): number => {
936
const findKth = (nums1: number[], nums2: number[], k: number): number => {
10-
var m: number = nums1.length;
11-
var n: number = nums2.length;
37+
let m: number = nums1.length;
38+
let n: number = nums2.length;
1239
if (m > n) {
1340
return findKth(nums2, nums1, k);
1441
}
@@ -18,23 +45,23 @@ var findMedianSortedArrays = (nums1: number[], nums2: number[]): number => {
1845
if (k === 1) {
1946
return Math.min(nums1[0], nums2[0]);
2047
}
21-
var pa = Math.floor(k / 2) < m ? Math.floor(k / 2) : m;
22-
var pb = k - pa;
48+
let pa = Math.floor(k / 2) < m ? Math.floor(k / 2) : m;
49+
let pb = k - pa;
2350
if (nums1[pa - 1] < nums2[pb - 1]) {
24-
var t1 = nums1.slice(pa);
51+
let t1 = nums1.slice(pa);
2552
return findKth(t1, nums2, k - pa);
2653
} else if (nums1[pa - 1] > nums2[pb - 1]) {
27-
var t2 = nums2.slice(pb);
54+
let t2 = nums2.slice(pb);
2855
//nums2.splice(0,pb);
2956
return findKth(nums1, t2, k - pb);
3057
} else {
3158
return nums1[pa - 1];
3259
}
3360
};
3461

35-
var m = nums1.length;
36-
var n = nums2.length;
37-
var tol = m + n;
62+
let m = nums1.length;
63+
let n = nums2.length;
64+
let tol = m + n;
3865
if (tol / 2 - Math.floor(tol / 2) > 0.1) {
3966
return findKth(nums1, nums2, Math.floor(tol / 2) + 1);
4067
} else {
@@ -45,3 +72,5 @@ var findMedianSortedArrays = (nums1: number[], nums2: number[]): number => {
4572
);
4673
}
4774
};
75+
76+
export default findMedianSortedArrays;

‎algorithms/0014.longest-common-prefix/index.ts

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,35 @@
1+
`
2+
14. Longest Common Prefix
3+
4+
Write a function to find the longest common prefix string amongst an array of strings.
5+
6+
If there is no common prefix, return an empty string \`""\`.
7+
8+
"""
9+
Input: ["flower","flow","flight"]
10+
Output: "fl"
11+
"""
12+
13+
"""
14+
Input: ["dog","racecar","car"]
15+
Output: ""
16+
Explanation: There is no common prefix among the input strings.
17+
"""
18+
19+
::: All given inputs are in lowercase letters \`a-z\`.
20+
21+
>>> String
22+
`;
23+
let longestCommonPrefix: (strs: string[]) => string;
24+
125
/**
226
* 循环遍历
327
* 刚拿到这题没有细想,认为这题比较简单;直接使用循环遍历的方式一遍一遍顺序访问数组。
428
* 当子字符串不相等时 Break,结果却超时了。简单了分析了原因在长数组的情况下,过多的进行了顺序访问,实践复杂度为 O(k\*n)。
529
* 便放弃广度优先,改为深读优先。后面发现,是长度为 1 的数组进入了死循环。
630
* @status Time Limit Exceeded
731
*/
8-
exportconstlongestCommonPrefix = (strs: string[]): string => {
32+
longestCommonPrefix = (strs: string[]): string => {
933
let prefix: string = '';
1034
if (strs && strs.length > 0) {
1135
let point = 1;
@@ -27,9 +51,9 @@ export const longestCommonPrefix = (strs: string[]): string => {
2751
* 单次遍历
2852
* 没有兼容好空字符串的问题(PS:条件不是 a-z 么?WTF?)
2953
* @status Time Limit Exceeded
30-
* @params `["",""]`
54+
* @case `["",""]`
3155
*/
32-
exportconstlongestCommonPrefix1 = (strs: string[]): string => {
56+
longestCommonPrefix = (strs: string[]): string => {
3357
let prefix: string = '';
3458
if (strs.length == 1) {
3559
return strs[0];
@@ -57,7 +81,7 @@ export const longestCommonPrefix1 = (strs: string[]): string => {
5781
* @runtime 60ms < 97.88%
5882
* @memory 33.8MB < 86.74%
5983
*/
60-
exportconstlongestCommonPrefix2 = (strs: string[]): string => {
84+
longestCommonPrefix = (strs: string[]): string => {
6185
if (strs && strs.length > 0) {
6286
let prefix: string = strs[0]; // 使用 strs[0] 作为初始前缀串
6387
for (let i = 1; i < strs.length; i++) {
@@ -75,3 +99,5 @@ export const longestCommonPrefix2 = (strs: string[]): string => {
7599
return '';
76100
}
77101
};
102+
103+
export default longestCommonPrefix;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /