Skip to content

Commit 9652acd

Browse files
committed
add two sum
1 parent f44b1ff commit 9652acd

File tree

6 files changed

+92
-13
lines changed

6 files changed

+92
-13
lines changed

README.md

+15-11
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@ JavaScript code[ES2015] for some classical algorithm
1212
npm run test
1313
```
1414

15+
### LeetCode Classical Problems
16+
17+
+ [Two Sum](./src/leetcode/two-sum.js) [Problem Details](https://leetcode.com/problems/two-sum/description/)
18+
1519
### List
1620

17-
+ [unique an array 数组去重]('./code/unique.js')
18-
+ [random string 随机字符串]('./code/random-string.js')
19-
+ [find duplicate char 寻找重复字符]('./code/find-the-max-duplicate-chat.js')
20-
+ [find the maxium value 寻找最大值]('./code/find-max.js')
21+
+ [unique an array 数组去重]('./src/unique.js')
22+
+ [random string 随机字符串]('./src/random-string.js')
23+
+ [find duplicate char 寻找重复字符]('./src/find-the-max-duplicate-chat.js')
24+
+ [find the maxium value 寻找最大值]('./src/find-max.js')
2125

2226
#### Sort Algorithms
2327

24-
+ [bubble Sort 冒泡排序]('./code/bubble-sort.js')
25-
+ [quick Sort 快速排序]('./code/quick-sort.js')
26-
+ [selection sort 选择排序]('./code/selection-sort.js')
27-
+ [shell sort 选择排序]('./code/shell-sort.js')
28-
+ [heap sort 堆排序]('./code/heap-sort.js')
28+
+ [bubble Sort 冒泡排序]('./src/bubble-sort.js')
29+
+ [quick Sort 快速排序]('./src/quick-sort.js')
30+
+ [selection sort 选择排序]('./src/selection-sort.js')
31+
+ [shell sort 选择排序]('./src/shell-sort.js')
32+
+ [heap sort 堆排序]('./src/heap-sort.js')
2933

30-
+ [fibonacci sequence 斐波那契数列]('./code/fibonacci-sequence-canvas.js')
31-
+ [binary search tree 二叉查找树](./code/binary-search-tree.js)
34+
+ [fibonacci sequence 斐波那契数列]('./src/fibonacci-sequence-canvas.js')
35+
+ [binary search tree 二叉查找树](./src/binary-search-tree.js)
3236

3337
### Reference
3438

coverage/coverage-final.json

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/insert-sort.js":{"path":"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/insert-sort.js","statementMap":{"0":{"start":{"line":6,"column":4},"end":{"line":14,"column":5}},"1":{"start":{"line":7,"column":16},"end":{"line":7,"column":17}},"2":{"start":{"line":8,"column":18},"end":{"line":8,"column":24}},"3":{"start":{"line":9,"column":8},"end":{"line":12,"column":9}},"4":{"start":{"line":10,"column":12},"end":{"line":10,"column":30}},"5":{"start":{"line":11,"column":12},"end":{"line":11,"column":16}},"6":{"start":{"line":13,"column":8},"end":{"line":13,"column":21}},"7":{"start":{"line":15,"column":4},"end":{"line":15,"column":15}},"8":{"start":{"line":18,"column":0},"end":{"line":18,"column":28}}},"fnMap":{"0":{"name":"insertSort","decl":{"start":{"line":5,"column":9},"end":{"line":5,"column":19}},"loc":{"start":{"line":5,"column":25},"end":{"line":16,"column":1}},"line":5}},"branchMap":{"0":{"loc":{"start":{"line":9,"column":14},"end":{"line":9,"column":31}},"type":"binary-expr","locations":[{"start":{"line":9,"column":14},"end":{"line":9,"column":17}},{"start":{"line":9,"column":19},"end":{"line":9,"column":31}}],"line":9}},"s":{"0":1,"1":7,"2":7,"3":7,"4":14,"5":14,"6":7,"7":1,"8":1},"f":{"0":1},"b":{"0":[21,20]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"2da37df4cd4e895852320ce6fd095542c9e7aa30","l":{"6":1,"7":7,"8":7,"9":7,"10":14,"11":14,"13":7,"15":1,"18":1}},
1414
"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/knapsack.test.js":{"path":"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/knapsack.test.js","statementMap":{"0":{"start":{"line":1,"column":17},"end":{"line":1,"column":46}},"1":{"start":{"line":2,"column":0},"end":{"line":8,"column":3}},"2":{"start":{"line":3,"column":20},"end":{"line":3,"column":22}},"3":{"start":{"line":4,"column":18},"end":{"line":4,"column":29}},"4":{"start":{"line":5,"column":19},"end":{"line":5,"column":33}},"5":{"start":{"line":7,"column":4},"end":{"line":7,"column":57}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":2,"column":24},"end":{"line":2,"column":25}},"loc":{"start":{"line":2,"column":36},"end":{"line":8,"column":1}},"line":2}},"branchMap":{},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1},"f":{"0":1},"b":{},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"f31fffbeb543fce00768114407563c4c49fda9a0","l":{"1":1,"2":1,"3":1,"4":1,"5":1,"7":1}},
1515
"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/knapsack.js":{"path":"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/knapsack.js","statementMap":{"0":{"start":{"line":5,"column":10},"end":{"line":5,"column":12}},"1":{"start":{"line":6,"column":2},"end":{"line":8,"column":3}},"2":{"start":{"line":7,"column":4},"end":{"line":7,"column":14}},"3":{"start":{"line":9,"column":2},"end":{"line":21,"column":3}},"4":{"start":{"line":10,"column":4},"end":{"line":20,"column":5}},"5":{"start":{"line":11,"column":6},"end":{"line":17,"column":7}},"6":{"start":{"line":12,"column":8},"end":{"line":12,"column":20}},"7":{"start":{"line":13,"column":12},"end":{"line":17,"column":7}},"8":{"start":{"line":14,"column":8},"end":{"line":14,"column":71}},"9":{"start":{"line":16,"column":8},"end":{"line":16,"column":28}},"10":{"start":{"line":23,"column":2},"end":{"line":23,"column":24}},"11":{"start":{"line":27,"column":0},"end":{"line":27,"column":26}}},"fnMap":{"0":{"name":"knapsack","decl":{"start":{"line":4,"column":9},"end":{"line":4,"column":17}},"loc":{"start":{"line":4,"column":44},"end":{"line":25,"column":1}},"line":4}},"branchMap":{"0":{"loc":{"start":{"line":11,"column":6},"end":{"line":17,"column":7}},"type":"if","locations":[{"start":{"line":11,"column":6},"end":{"line":17,"column":7}},{"start":{"line":11,"column":6},"end":{"line":17,"column":7}}],"line":11},"1":{"loc":{"start":{"line":11,"column":9},"end":{"line":11,"column":21}},"type":"binary-expr","locations":[{"start":{"line":11,"column":9},"end":{"line":11,"column":13}},{"start":{"line":11,"column":17},"end":{"line":11,"column":21}}],"line":11},"2":{"loc":{"start":{"line":13,"column":12},"end":{"line":17,"column":7}},"type":"if","locations":[{"start":{"line":13,"column":12},"end":{"line":17,"column":7}},{"start":{"line":13,"column":12},"end":{"line":17,"column":7}}],"line":13}},"s":{"0":1,"1":1,"2":6,"3":1,"4":6,"5":102,"6":22,"7":80,"8":54,"9":26,"10":1,"11":1},"f":{"0":1},"b":{"0":[22,80],"1":[102,85],"2":[54,26]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"20bf84d9baa8b6bfce02aaf3e993c3d359906545","l":{"5":1,"6":1,"7":6,"9":1,"10":6,"11":102,"12":22,"13":80,"14":54,"16":26,"23":1,"27":1}},
16+
"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/leetcode/two-num.test.js":{"path":"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/leetcode/two-num.test.js","statementMap":{"0":{"start":{"line":7,"column":15},"end":{"line":7,"column":55}},"1":{"start":{"line":8,"column":0},"end":{"line":14,"column":3}},"2":{"start":{"line":9,"column":21},"end":{"line":9,"column":35}},"3":{"start":{"line":10,"column":19},"end":{"line":10,"column":20}},"4":{"start":{"line":11,"column":19},"end":{"line":11,"column":43}},"5":{"start":{"line":12,"column":4},"end":{"line":12,"column":33}},"6":{"start":{"line":13,"column":4},"end":{"line":13,"column":33}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":8,"column":14},"end":{"line":8,"column":15}},"loc":{"start":{"line":8,"column":26},"end":{"line":14,"column":1}},"line":8}},"branchMap":{},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":1,"6":1},"f":{"0":1},"b":{},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"acc71e370a7a15f392bd54c7a8f8aaab3c3de08e","l":{"7":1,"8":1,"9":1,"10":1,"11":1,"12":1,"13":1}},
17+
"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/leetcode/two-sum.js":{"path":"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/leetcode/two-sum.js","statementMap":{"0":{"start":{"line":2,"column":0},"end":{"line":15,"column":2}},"1":{"start":{"line":3,"column":16},"end":{"line":3,"column":20}},"2":{"start":{"line":4,"column":12},"end":{"line":4,"column":13}},"3":{"start":{"line":5,"column":12},"end":{"line":5,"column":26}},"4":{"start":{"line":6,"column":4},"end":{"line":13,"column":5}},"5":{"start":{"line":7,"column":20},"end":{"line":7,"column":35}},"6":{"start":{"line":8,"column":8},"end":{"line":12,"column":9}},"7":{"start":{"line":9,"column":12},"end":{"line":9,"column":19}},"8":{"start":{"line":10,"column":14},"end":{"line":12,"column":9}},"9":{"start":{"line":11,"column":12},"end":{"line":11,"column":19}},"10":{"start":{"line":14,"column":4},"end":{"line":14,"column":18}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":2,"column":17},"end":{"line":2,"column":18}},"loc":{"start":{"line":2,"column":41},"end":{"line":15,"column":1}},"line":2}},"branchMap":{"0":{"loc":{"start":{"line":8,"column":8},"end":{"line":12,"column":9}},"type":"if","locations":[{"start":{"line":8,"column":8},"end":{"line":12,"column":9}},{"start":{"line":8,"column":8},"end":{"line":12,"column":9}}],"line":8},"1":{"loc":{"start":{"line":10,"column":14},"end":{"line":12,"column":9}},"type":"if","locations":[{"start":{"line":10,"column":14},"end":{"line":12,"column":9}},{"start":{"line":10,"column":14},"end":{"line":12,"column":9}}],"line":10}},"s":{"0":1,"1":1,"2":1,"3":1,"4":1,"5":2,"6":2,"7":0,"8":2,"9":2,"10":1},"f":{"0":1},"b":{"0":[0,2],"1":[2,0]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"e1abb600684c0b3e4b0be9bdf2c31cef044b6124","l":{"2":1,"3":1,"4":1,"5":1,"6":1,"7":2,"8":2,"9":0,"10":2,"11":2,"14":1}},
1618
"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/quick-sort.test.js":{"path":"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/quick-sort.test.js","statementMap":{"0":{"start":{"line":1,"column":18},"end":{"line":1,"column":49}},"1":{"start":{"line":2,"column":0},"end":{"line":6,"column":3}},"2":{"start":{"line":3,"column":21},"end":{"line":3,"column":47}},"3":{"start":{"line":4,"column":22},"end":{"line":4,"column":41}},"4":{"start":{"line":5,"column":4},"end":{"line":5,"column":59}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":2,"column":18},"end":{"line":2,"column":19}},"loc":{"start":{"line":2,"column":30},"end":{"line":6,"column":1}},"line":2}},"branchMap":{},"s":{"0":1,"1":1,"2":1,"3":1,"4":1},"f":{"0":1},"b":{},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"b35f50bf19fc085ba9048a3d6faf815651727ef9","l":{"1":1,"2":1,"3":1,"4":1,"5":1}},
1719
"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/quick-sort.js":{"path":"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/quick-sort.js","statementMap":{"0":{"start":{"line":6,"column":4},"end":{"line":8,"column":5}},"1":{"start":{"line":7,"column":8},"end":{"line":7,"column":19}},"2":{"start":{"line":10,"column":18},"end":{"line":10,"column":20}},"3":{"start":{"line":11,"column":19},"end":{"line":11,"column":21}},"4":{"start":{"line":12,"column":12},"end":{"line":12,"column":18}},"5":{"start":{"line":13,"column":4},"end":{"line":19,"column":5}},"6":{"start":{"line":14,"column":8},"end":{"line":18,"column":9}},"7":{"start":{"line":15,"column":12},"end":{"line":15,"column":34}},"8":{"start":{"line":17,"column":12},"end":{"line":17,"column":33}},"9":{"start":{"line":21,"column":4},"end":{"line":21,"column":65}},"10":{"start":{"line":25,"column":0},"end":{"line":25,"column":27}}},"fnMap":{"0":{"name":"quickSort","decl":{"start":{"line":4,"column":9},"end":{"line":4,"column":18}},"loc":{"start":{"line":4,"column":24},"end":{"line":22,"column":1}},"line":4}},"branchMap":{"0":{"loc":{"start":{"line":6,"column":4},"end":{"line":8,"column":5}},"type":"if","locations":[{"start":{"line":6,"column":4},"end":{"line":8,"column":5}},{"start":{"line":6,"column":4},"end":{"line":8,"column":5}}],"line":6},"1":{"loc":{"start":{"line":14,"column":8},"end":{"line":18,"column":9}},"type":"if","locations":[{"start":{"line":14,"column":8},"end":{"line":18,"column":9}},{"start":{"line":14,"column":8},"end":{"line":18,"column":9}}],"line":14}},"s":{"0":11,"1":6,"2":5,"3":5,"4":5,"5":5,"6":14,"7":10,"8":4,"9":5,"10":1},"f":{"0":11},"b":{"0":[6,5],"1":[10,4]},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"d19d6f941ad7acfa3f536aaf2a238abdb2ce3f88","l":{"6":11,"7":6,"10":5,"11":5,"12":5,"13":5,"14":14,"15":10,"17":4,"21":5,"25":1}},
1820
"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/random-string.test.js":{"path":"/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/random-string.test.js","statementMap":{"0":{"start":{"line":1,"column":21},"end":{"line":1,"column":55}},"1":{"start":{"line":2,"column":0},"end":{"line":5,"column":3}},"2":{"start":{"line":3,"column":16},"end":{"line":3,"column":31}},"3":{"start":{"line":4,"column":4},"end":{"line":4,"column":34}}},"fnMap":{"0":{"name":"(anonymous_0)","decl":{"start":{"line":2,"column":32},"end":{"line":2,"column":33}},"loc":{"start":{"line":2,"column":44},"end":{"line":5,"column":1}},"line":2}},"branchMap":{},"s":{"0":1,"1":1,"2":1,"3":1},"f":{"0":1},"b":{},"_coverageSchema":"332fd63041d2c1bcb487cc26dd0d5f7d97098a6c","hash":"df0ff48d776a44ad1d1aa2c2f94bc7760d3e7e24","l":{"1":1,"2":1,"3":1,"4":1}},

coverage/lcov.info

+44
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,50 @@ BRF:6
487487
BRH:6
488488
end_of_record
489489
TN:
490+
SF:/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/leetcode/two-num.test.js
491+
FN:8,(anonymous_0)
492+
FNF:1
493+
FNH:1
494+
FNDA:1,(anonymous_0)
495+
DA:7,1
496+
DA:8,1
497+
DA:9,1
498+
DA:10,1
499+
DA:11,1
500+
DA:12,1
501+
DA:13,1
502+
LF:7
503+
LH:7
504+
BRF:0
505+
BRH:0
506+
end_of_record
507+
TN:
508+
SF:/Users/ali-130257n/www/JavaScript-Algorithm-Learning/src/leetcode/two-sum.js
509+
FN:2,(anonymous_0)
510+
FNF:1
511+
FNH:1
512+
FNDA:1,(anonymous_0)
513+
DA:2,1
514+
DA:3,1
515+
DA:4,1
516+
DA:5,1
517+
DA:6,1
518+
DA:7,2
519+
DA:8,2
520+
DA:9,0
521+
DA:10,2
522+
DA:11,2
523+
DA:14,1
524+
LF:11
525+
LH:10
526+
BRDA:8,0,0,0
527+
BRDA:8,0,1,2
528+
BRDA:10,1,0,2
529+
BRDA:10,1,1,0
530+
BRF:4
531+
BRH:2
532+
end_of_record
533+
TN:
490534
SF:/Users/ali-130257n/www/JavaScript-Algorithm-Learning/tests/quick-sort.test.js
491535
FN:2,(anonymous_0)
492536
FNF:1

karma.conf.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ module.exports = function(config) {
1717
files: [
1818
// './node_modules/babel-polyfill/dist/polyfill.min.js',
1919
//'src/*.js',
20-
'tests/*.test.js'
20+
'tests/**/*.test.js'
2121
],
2222
webpack: {
2323
module: {
@@ -38,7 +38,7 @@ module.exports = function(config) {
3838

3939
],
4040
preprocessors: {
41-
'tests/*.test.js': ['webpack'],
41+
'tests/**/*.test.js': ['webpack'],
4242
'src/*.js': ['coverage'],
4343
},
4444

src/leetcode/two-sum.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// https://leetcode.com/problems/two-sum/description/
2+
module.exports = function (nums, target) {
3+
const arr = nums;
4+
let i = 0;
5+
let j = arr.length - 1;
6+
while(arr[i] + arr[j] !== target) {
7+
const val = arr[i] + arr[j];
8+
if (val < target) {
9+
i += 1;
10+
}else if(val > target) {
11+
j -= 1;
12+
}
13+
}
14+
return [i, j];
15+
};

tests/leetcode/two-num.test.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* https://leetcode.com/problems/two-sum/description/
3+
* Given nums = [2, 7, 11, 15], target = 9,
4+
* Because nums[0] + nums[1] = 2 + 7 = 9,
5+
* return [0, 1].
6+
*/
7+
const twoSum = require('../../src/leetcode/two-sum.js');
8+
it('two sum', function () {
9+
const inputArr = [2, 7, 11, 15];
10+
const target = 9;
11+
const output = twoSum(inputArr, target)
12+
expect(output[0]).toEqual(0);
13+
expect(output[1]).toEqual(1);
14+
});

0 commit comments

Comments
 (0)