From 346a5a4f92538a4615e8b7c5caee2114e9603c7f Mon Sep 17 00:00:00 2001 From: mcn <2210109360@qq.com> Date: Sun, 21 Oct 2018 23:13:05 +0800 Subject: [PATCH 1/2] Update solution 001 Solution.js Update solution 001 Solution2.js --- solution/001.Two Sum/README.md | 2 +- solution/001.Two Sum/Solution2.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) create mode 100644 solution/001.Two Sum/Solution2.js diff --git a/solution/001.Two Sum/README.md b/solution/001.Two Sum/README.md index 930bbc64d1b90..9dd107cb1090b 100644 --- a/solution/001.Two Sum/README.md +++ b/solution/001.Two Sum/README.md @@ -29,4 +29,4 @@ class Solution { return null; } } -``` \ No newline at end of file +``` diff --git a/solution/001.Two Sum/Solution2.js b/solution/001.Two Sum/Solution2.js new file mode 100644 index 0000000000000..3c4b7abb4f047 --- /dev/null +++ b/solution/001.Two Sum/Solution2.js @@ -0,0 +1,10 @@ +var twoSum = function(nums, target) { + var len = nums.length; + var n = {}; + for(var i = 0; i < len; i++){ + if(n[target - nums[i]] !== undefined){ + return [n[target - nums[i]], i]; + } + n[nums[i]] = i; + } +}; From 8240d37d6d141da27b76eeb21877bf836462cc50 Mon Sep 17 00:00:00 2001 From: mcn <2210109360@qq.com> Date: Mon, 22 Oct 2018 21:22:57 +0800 Subject: [PATCH 2/2] update solution 002 Solution.js [JavaScript] --- solution/002.Add Two Numbers/Solution.js | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 solution/002.Add Two Numbers/Solution.js diff --git a/solution/002.Add Two Numbers/Solution.js b/solution/002.Add Two Numbers/Solution.js new file mode 100644 index 0000000000000..5f94782a4f988 --- /dev/null +++ b/solution/002.Add Two Numbers/Solution.js @@ -0,0 +1,36 @@ +/** + * 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) { + var c1 = l1,c2 = l2,c3,l3,carry = 0; + while(c1||c2||carry){ + var v1 = 0,v2 = 0; + if(c1){ + v1 = c1.val; + c1 = c1.next; + } + if(c2){ + v2 = c2.val; + c2 = c2.next; + } + var sum = v1 + v2 + carry; + carry = (sum - sum%10)/10; + if(!c3){ + l3 = new ListNode(sum%10); + c3 = l3; + }else{ + c3.next = new ListNode(sum%10); + c3 = c3.next; + } + } + return l3; +} \ No newline at end of file