Skip to content

Commit 272cb1c

Browse files
author
zongyanqi
committedDec 3, 2017
format filenames
1 parent b768505 commit 272cb1c

File tree

82 files changed

+2824
-259
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+2824
-259
lines changed
 

‎001-Two-Sum.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* https://leetcode.com/problems/two-sum/description/
3+
* Difficulty:Easy
4+
*
5+
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
6+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
7+
* Example:
8+
* Given nums = [2, 7, 11, 15], target = 9,
9+
* Because nums[0] + nums[1] = 2 + 7 = 9,
10+
* return [0, 1].
11+
*/
12+
13+
/**
14+
* @param {number[]} numbers
15+
* @param {number} target
16+
* @return {number[]}
17+
*/
18+
var twoSum = function (numbers, target) {
19+
20+
for (var i = 0; i < numbers.length - 1; i++) {
21+
for (var j = i + 1; j < numbers.length; j++) {
22+
if (numbers[i] + numbers[j] === target) return [i, j];
23+
}
24+
}
25+
};
26+
27+
var twoSum2 = function (numbers, target) {
28+
var map = {};
29+
for (var i = 0; i < numbers.length; i++) {
30+
var n = numbers[i];
31+
if (map[target - n] !== undefined) {
32+
return [map[target - n], i];
33+
} else {
34+
map[n] = i;
35+
}
36+
}
37+
};
38+
39+
console.log(twoSum([2, 11, 15, 7], 9));
40+
console.log(twoSum2([2, 7, 11, 15], 9));
41+
console.log(twoSum2([2, 7, 11, 15], 26));
42+
console.log(twoSum2([2, 7, 11, 15], 26));
43+

‎002-Add-Two-Numbers.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* https://leetcode.com/problems/add-two-numbers/description/
3+
* Difficulty:Medium
4+
*
5+
* You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
6+
* You may assume the two numbers do not contain any leading zero, except the number 0 itself.
7+
* Example
8+
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
9+
* Output: 7 -> 0 -> 8
10+
* Explanation: 342 + 465 = 807.
11+
*/
12+
13+
// Definition for singly-linked list.
14+
function ListNode(val) {
15+
this.val = val;
16+
this.next = null;
17+
}
18+
19+
/**
20+
* @param {ListNode} l1
21+
* @param {ListNode} l2
22+
* @return {ListNode}
23+
*/
24+
var addTwoNumbers = function (l1, l2) {
25+
26+
var c = 0;
27+
var ret = new ListNode(0);
28+
var curr = ret;
29+
30+
while (l1 || l2) {
31+
var a = l1 ? l1.val : 0;
32+
var b = l2 ? l2.val : 0;
33+
var sum = a + b + c;
34+
c = Math.floor(sum / 10);
35+
curr.next = new ListNode(sum % 10);
36+
if (l1) {
37+
l1 = l1.next;
38+
}
39+
if (l2) {
40+
l2 = l2.next;
41+
}
42+
curr = curr.next;
43+
}
44+
if (c) {
45+
curr.next = new ListNode(c);
46+
}
47+
48+
return ret.next;
49+
};

0 commit comments

Comments
 (0)