Skip to content

Commit 806e470

Browse files
committed
✨ 20210601
1 parent ef668de commit 806e470

File tree

7 files changed

+138
-0
lines changed

7 files changed

+138
-0
lines changed

TODO.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,9 @@
5050
- [ ] Add Discuss 31
5151
- [ ] 实现冒泡排序
5252
- [ ] 实现选择排序
53+
54+
206
55+
3
56+
2
57+
5
58+
19

src/1-two-sum/20210601.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function (nums, target) {
7+
const hash = {};
8+
9+
for (let i = 0; i < nums.length; i++) {
10+
if (hash[nums[i]] !== undefined) {
11+
return [hash[nums[i]], i];
12+
}
13+
hash[target - nums[i]] = i;
14+
}
15+
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var removeNthFromEnd = function (head, n) {
2+
let preHead = new ListNode(0);
3+
preHead.next = head;
4+
let fast = preHead;
5+
let slow = preHead;
6+
7+
// 快先走 n+1 步
8+
while (n--) {
9+
fast = fast.next;
10+
}
11+
// fast、slow 一起前进
12+
while (fast && fast.next) {
13+
fast = fast.next;
14+
slow = slow.next;
15+
}
16+
slow.next = slow.next.next;
17+
return preHead.next;
18+
};

src/2-add-two-numbers/20210601.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val) {
4+
* this.val = val;
5+
* this.next = null;
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} l1
10+
* @param {ListNode} l2
11+
* @return {ListNode}
12+
*/
13+
var addTwoNumbers = function (l1, l2) {
14+
const pre = new ListNode();
15+
let cur = pre;
16+
let carry = 0;
17+
18+
while (l1 || l2) {
19+
const sum = (l1 ? l1.val : 0) + (l2 ? l2.val : 0) + carry;
20+
carry = sum >= 10 ? 1 : 0;
21+
cur.next = new ListNode(sum % 10);
22+
cur = cur.next;
23+
24+
l1 && (l1 = l1.next);
25+
l2 && (l2 = l2.next);
26+
}
27+
28+
carry && (cur.next = new ListNode(carry));
29+
30+
return pre.next;
31+
};

src/20-valid-parentheses/20210601.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @param {string} s
3+
* @return {boolean}
4+
*/
5+
var isValid = function (s) {
6+
const hash = {
7+
')': '(',
8+
']': '[',
9+
'}': '{',
10+
};
11+
12+
const stack = [];
13+
for (let i = 0; i < s.length; i++) {
14+
const cur = stack.pop();
15+
if (cur === undefined) {
16+
stack.push(s[i]);
17+
continue;
18+
}
19+
if (hash[s[i]] !== cur) {
20+
stack.push(cur);
21+
stack.push(s[i]);
22+
}
23+
}
24+
25+
return stack.length === 0;
26+
};

src/206-reverse-linked-list/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
/**
9+
* @param {ListNode} head
10+
* @return {ListNode}
11+
*/
12+
var reverseList = function (head) {
13+
let [reverse, cur] = [null, head];
14+
15+
while (cur) {
16+
const next = cur.next;
17+
cur.next = reverse;
18+
reverse = cur;
19+
cur = next;
20+
}
21+
22+
return reverse;
23+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function (s) {
6+
let res = 0;
7+
let start = 0;
8+
const map = new Map();
9+
10+
for (let i = 0; i < s.length; i++) {
11+
if (map.has(s[i])) {
12+
start = Math.max(start, map.get(s[i]));
13+
}
14+
res = Math.max(res, i - start + 1);
15+
map.set(s[i], i + 1);
16+
}
17+
18+
return res;
19+
};

0 commit comments

Comments
 (0)