Skip to content

Commit 4928e14

Browse files
committed
update solution
1 parent 191d3dc commit 4928e14

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

solution/0001.Two Sum/Solution3.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var twoSum = function(nums, target) {
2+
const map = new Map()
3+
for(let i = 0; i < nums.length; i++){
4+
if (map.has(target - nums[i])){
5+
return [ map.get(target - nums[i]), i ]
6+
}
7+
map.set(nums[i], i)
8+
}
9+
};
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
let head = new ListNode(0)
15+
let cur = head
16+
let curry = 0
17+
18+
while (true) {
19+
let sum = curry
20+
sum += l1 ? l1.val : 0
21+
sum += l2 ? l2.val : 0
22+
cur.val = sum % 10
23+
curry = parseInt(sum / 10)
24+
if (l1) l1 = l1.next
25+
if (l2) l2 = l2.next
26+
if (l1 != null || l2 != null) {
27+
cur.next = new ListNode(0)
28+
cur = cur.next
29+
} else {
30+
break
31+
}
32+
}
33+
if (curry != 0) {
34+
cur.next = new ListNode(0)
35+
cur = cur.next
36+
cur.val = curry
37+
}
38+
return head
39+
};
40+
41+
var l1 = new ListNode(1)
42+
l1.next = new ListNode(8)
43+
44+
var l2 = new ListNode(0)
45+
46+
console.log(addTwoNumbers(l1, l2))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var lengthOfLongestSubstring = function(s) {
6+
var start = 0; // 非重复字符串开始索引
7+
var max = 0; // 最长字符串长度
8+
var visitedCharByPosition = {};
9+
for (var position = 0; position < s.length; position++) {
10+
var nextChar = s[position];
11+
if (nextChar in visitedCharByPosition && visitedCharByPosition[nextChar] >= start) {
12+
// 有重复,非重复字符串索引从下一个 index 开始
13+
start = visitedCharByPosition[nextChar] + 1;
14+
visitedCharByPosition[nextChar] = position;
15+
} else {
16+
visitedCharByPosition[nextChar] = position;
17+
// 非重复,求非重复值
18+
max = Math.max(max, position + 1 - start);
19+
}
20+
}
21+
return max;
22+
};

0 commit comments

Comments
 (0)