Skip to content

update solution #135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions solution/0001.Two Sum/Solution3.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var twoSum = function(nums, target) {
const map = new Map()
for(let i = 0; i < nums.length; i++){
if (map.has(target - nums[i])){
return [ map.get(target - nums[i]), i ]
}
map.set(nums[i], i)
}
};
46 changes: 46 additions & 0 deletions solution/0002.Add Two Numbers/Solution2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
*/
var addTwoNumbers = function(l1, l2) {
let head = new ListNode(0)
let cur = head
let curry = 0

while (true) {
let sum = curry
sum += l1 ? l1.val : 0
sum += l2 ? l2.val : 0
cur.val = sum % 10
curry = parseInt(sum / 10)
if (l1) l1 = l1.next
if (l2) l2 = l2.next
if (l1 != null || l2 != null) {
cur.next = new ListNode(0)
cur = cur.next
} else {
break
}
}
if (curry != 0) {
cur.next = new ListNode(0)
cur = cur.next
cur.val = curry
}
return head
};

var l1 = new ListNode(1)
l1.next = new ListNode(8)

var l2 = new ListNode(0)

console.log(addTwoNumbers(l1, l2))
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @param {string} s
* @return {number}
*/
var lengthOfLongestSubstring = function(s) {
var start = 0; // 非重复字符串开始索引
var max = 0; // 最长字符串长度
var visitedCharByPosition = {};
for (var position = 0; position < s.length; position++) {
var nextChar = s[position];
if (nextChar in visitedCharByPosition && visitedCharByPosition[nextChar] >= start) {
// 有重复,非重复字符串索引从下一个 index 开始
start = visitedCharByPosition[nextChar] + 1;
visitedCharByPosition[nextChar] = position;
} else {
visitedCharByPosition[nextChar] = position;
// 非重复,求非重复值
max = Math.max(max, position + 1 - start);
}
}
return max;
};