Skip to content

Commit 2d8238b

Browse files
committed
update
1 parent 97b28a3 commit 2d8238b

7 files changed

+446
-0
lines changed

001-100/18. 4Sum.md

+48
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,55 @@ A solution set is:
2828
## Solution
2929

3030
```javascript
31+
/**
32+
* @param {number[]} nums
33+
* @param {number} target
34+
* @return {number[][]}
35+
*/
36+
var fourSum = function(nums, target) {
37+
if (nums.length < 4) return [];
3138

39+
var len = nums.length;
40+
var res = [];
41+
var l = 0;
42+
var r = 0;
43+
var sum = 0;
44+
45+
nums.sort((a, b) => (a - b));
46+
47+
for (var i = 0; i < len - 3; i++) {
48+
if (i > 0 && nums[i] === nums[i - 1]) continue;
49+
if (nums[i] + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) break;
50+
if (nums[i] + nums[len - 1] + nums[len - 2] + nums[len - 3] < target) continue;
51+
52+
for (var j = i + 1; j < len - 2; j++) {
53+
if (j > i + 1 && nums[j] === nums[j - 1]) continue;
54+
if (nums[i] + nums[j] + nums[j + 1] + nums[j + 2] > target) break;
55+
if (nums[i] + nums[j] + nums[len - 1] + nums[len - 2] < target) continue;
56+
57+
l = j + 1;
58+
r = len - 1;
59+
60+
while (l < r) {
61+
sum = nums[i] + nums[j] + nums[l] + nums[r];
62+
63+
if (sum < target) {
64+
l++;
65+
} else if (sum > target) {
66+
r--;
67+
} else {
68+
res.push([nums[i], nums[j], nums[l], nums[r]]);
69+
while (l < r && nums[l] === nums[l + 1]) l++;
70+
while (l < r && nums[r] === nums[r - 1]) r--;
71+
l++;
72+
r--;
73+
}
74+
}
75+
}
76+
}
77+
78+
return res;
79+
};
3280
```
3381

3482
**Explain:**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 19. Remove Nth Node From End of List
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Linked List, Two Pointers.
5+
- Similar Questions: .
6+
7+
## Problem
8+
9+
Given a linked list, remove the **n**-th node from the end of list and return its head.
10+
11+
**Example:**
12+
13+
```
14+
Given linked list: 1->2->3->4->5, and n = 2.
15+
16+
After removing the second node from the end, the linked list becomes 1->2->3->5.
17+
```
18+
19+
**Note:**
20+
21+
Given **n** will always be valid.
22+
23+
**Follow up:**
24+
25+
Could you do this in one pass?
26+
27+
## Solution
28+
29+
```javascript
30+
/**
31+
* Definition for singly-linked list.
32+
* function ListNode(val) {
33+
* this.val = val;
34+
* this.next = null;
35+
* }
36+
*/
37+
/**
38+
* @param {ListNode} head
39+
* @param {number} n
40+
* @return {ListNode}
41+
*/
42+
var removeNthFromEnd = function(head, n) {
43+
var h = new ListNode(0);
44+
var ll = h;
45+
var rr = h;
46+
47+
h.next = head;
48+
49+
for (var i = 0; i < n + 1; i++) {
50+
rr = rr.next;
51+
}
52+
53+
while (rr !== null) {
54+
ll = ll.next;
55+
rr= rr.next;
56+
}
57+
58+
ll.next = ll.next.next;
59+
60+
return h.next;
61+
};
62+
```
63+
64+
**Explain:**
65+
66+
1. 两个指针 ```a```, ```b```,初始都指向开头
67+
2.```b``` 指针往后移动,直到两指针的位置相差 ```n```
68+
3. 同时移动两指针,直到 ```b``` 指针到了最后。这时候因为两指针的位置相差 ```n``````a``` 指针的位置就是从后面数第 ```n+1```
69+
4.```a``` 指针那里删掉后面一个,即第 ```n```
70+
71+
要注意可能会出现删掉 ```head``` 的情况。
72+
73+
**Complexity:**
74+
75+
* Time complexity : O(n).
76+
* Space complexity : O(1).

001-100/20. Valid Parentheses.md

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# 20. Valid Parentheses
2+
3+
- Difficulty: Easy.
4+
- Related Topics: String, Stack.
5+
- Similar Questions: Generate Parentheses, Longest Valid Parentheses, Remove Invalid Parentheses.
6+
7+
## Problem
8+
9+
Given a string containing just the characters ```'('```, ```')'```, ```'{'```, ```'}'```, ```'['``` and ```']'```, determine if the input string is valid.
10+
11+
An input string is valid if:
12+
13+
- Open brackets must be closed by the same type of brackets.
14+
- Open brackets must be closed in the correct order.
15+
16+
Note that an empty string is also considered valid.
17+
18+
**Example 1:**
19+
20+
```
21+
Input: "()"
22+
Output: true
23+
```
24+
25+
**Example 2:**
26+
27+
```
28+
Input: "()[]{}"
29+
Output: true
30+
```
31+
32+
**Example 3:**
33+
34+
```
35+
Input: "(]"
36+
Output: false
37+
```
38+
39+
**Example 4:**
40+
41+
```
42+
Input: "([)]"
43+
Output: false
44+
```
45+
46+
**Example 5:**
47+
48+
```
49+
Input: "{[]}"
50+
Output: true
51+
```
52+
53+
## Solution
54+
55+
```javascript
56+
/**
57+
* @param {string} s
58+
* @return {boolean}
59+
*/
60+
var isValid = function(s) {
61+
var stack = [];
62+
var len = s.length;
63+
var map = {
64+
'(': ')',
65+
'[': ']',
66+
'{': '}'
67+
};
68+
for (var i = 0; i < len; i++) {
69+
if (stack.length > 0 && map[stack[stack.length - 1]] === s[i]) {
70+
stack.pop();
71+
} else {
72+
stack.push(s[i]);
73+
}
74+
}
75+
return stack.length === 0;
76+
};
77+
```
78+
79+
**Explain:**
80+
81+
nope.
82+
83+
**Complexity:**
84+
85+
* Time complexity : O(n).
86+
* Space complexity : O(n).

001-100/21. Merge Two Sorted Lists.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 21. Merge Two Sorted Lists
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Linked List.
5+
- Similar Questions: Merge k Sorted Lists, Merge Sorted Array, Sort List, Shortest Word Distance II.
6+
7+
## Problem
8+
9+
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
10+
11+
**Example:**
12+
```
13+
Input: 1->2->4, 1->3->4
14+
Output: 1->1->2->3->4->4
15+
```
16+
17+
## Solution
18+
19+
```javascript
20+
/**
21+
* Definition for singly-linked list.
22+
* function ListNode(val) {
23+
* this.val = val;
24+
* this.next = null;
25+
* }
26+
*/
27+
/**
28+
* @param {ListNode} l1
29+
* @param {ListNode} l2
30+
* @return {ListNode}
31+
*/
32+
var mergeTwoLists = function(l1, l2) {
33+
var head = new ListNode(0);
34+
var now = head;
35+
var p1 = l1;
36+
var p2 = l2;
37+
while (p1 || p2) {
38+
if (p1 === null || (p2 !== null && p2.val < p1.val)) {
39+
now.next = p2;
40+
p2 = p2.next;
41+
} else {
42+
now.next = p1;
43+
p1 = p1.next;
44+
}
45+
now = now.next;
46+
}
47+
return head.next;
48+
};
49+
```
50+
51+
**Explain:**
52+
53+
nope.
54+
55+
**Complexity:**
56+
57+
* Time complexity : O(m+n).
58+
* Space complexity : O(m+n).

001-100/22. Generate Parentheses.md

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# 22. Generate Parentheses
2+
3+
- Difficulty: Medium.
4+
- Related Topics: String, Backtracking.
5+
- Similar Questions: Letter Combinations of a Phone Number, Valid Parentheses.
6+
7+
## Problem
8+
9+
Given *n* pairs of parentheses, write a function to generate all combinations of well-formed parentheses.
10+
11+
For example, given *n* = 3, a solution set is:
12+
13+
```
14+
[
15+
"((()))",
16+
"(()())",
17+
"(())()",
18+
"()(())",
19+
"()()()"
20+
]
21+
```
22+
23+
## Solution
24+
25+
```javascript
26+
/**
27+
* @param {number} n
28+
* @return {string[]}
29+
*/
30+
var generateParenthesis = function(n) {
31+
var res = [];
32+
if (n < 1) return res;
33+
generate(res, '', n, n);
34+
return res;
35+
};
36+
37+
var generate = function (res, str, ll, rr) {
38+
if (ll || rr) {
39+
if (rr > ll) generate(res, str + ')', ll, rr - 1);
40+
if (ll) generate(res, str + '(', ll - 1, rr);
41+
} else {
42+
res.push(str);
43+
}
44+
};
45+
```
46+
47+
**Explain:**
48+
49+
每一层最多两种情况:
50+
51+
1. 右括号比左括号多,加右括号
52+
2. 还有左括号,加左括号
53+
54+
**Complexity:**
55+
56+
* Time complexity : O((4^n)/(n^(-1))).
57+
* Space complexity : O((4^n)/(n^(-1))).

401-500/415. Add Strings.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# 415. Add Strings
2+
3+
- Difficulty: Easy.
4+
- Related Topics: Math.
5+
- Similar Questions: Add Two Numbers, Multiply Strings.
6+
7+
## Problem
8+
9+
Given two non-negative integers ```num1``` and ```num2``` represented as string, return the sum of ```num1``` and ```num2```.
10+
11+
**Note:**
12+
13+
- The length of both ```num1``` and ```num2``` is < 5100.
14+
- Both ```num1``` and ```num2``` contains only digits ```0-9```.
15+
- Both ```num1``` and ```num2``` does not contain any leading zero.
16+
- You **must not use any built-in BigInteger library** or **convert the inputs to integer** directly.
17+
18+
## Solution
19+
20+
```javascript
21+
/**
22+
* @param {string} num1
23+
* @param {string} num2
24+
* @return {string}
25+
*/
26+
var addStrings = function(num1, num2) {
27+
var len1 = num1.length;
28+
var len2 = num2.length;
29+
var max = Math.max(len1, len2);
30+
var res = Array(max);
31+
var carry = 0;
32+
var val = 0;
33+
34+
for (var i = 0; i < max; i++) {
35+
val = Number(num1[len1 - 1 - i] || 0) + Number(num2[len2 - 1 - i] || 0) + carry;
36+
carry = Math.floor(val / 10);
37+
res[max - 1 - i] = val % 10;
38+
}
39+
40+
return (carry || '') + res.join('');
41+
};
42+
```
43+
44+
**Explain:**
45+
46+
nope.
47+
48+
**Complexity:**
49+
50+
* Time complexity :
51+
* Space complexity :

0 commit comments

Comments
 (0)