Skip to content

Commit 75a59ee

Browse files
committed
updated readme file and added jest test enviroment
1 parent 1f8d2f1 commit 75a59ee

File tree

4 files changed

+144
-0
lines changed

4 files changed

+144
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Given an array of integers, return an array of indices of the two numbers
2+
// that add up to the target.
3+
// You may assume that each input would have exactly one solution, and
4+
// you may not use the same element twice.
5+
// --- Examples
6+
// twoSum([2,7,11,15], 9) --> [0,1]
7+
// twoSum([3,2,4], 9) --> [1,2]
8+
9+
function twoSum(arr, target) {
10+
let numsVisited = {};
11+
let res = [];
12+
13+
for (let i = 0; i < arr.length; i++) {
14+
const num = arr[i];
15+
const complement = target - num;
16+
17+
if (numsVisited[complement] !== undefined) {
18+
res.push(i);
19+
res.push(numsVisited[complement]);
20+
}
21+
22+
numsVisited[num] = i;
23+
}
24+
25+
return res;
26+
}
27+
28+
module.exports = twoSum;

2020/01_January/LC1_TwoSum/test.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const twoSum = require('./index');
2+
3+
test('twoSum([2,7,11,15], 9) returns [0,1]', () => {
4+
expect(twoSum([2, 7, 11, 15], 9).sort((a, b) => (a < b ? -1 : 1))).toEqual([
5+
0,
6+
1
7+
]);
8+
});
9+
10+
test('twoSum([3, 2, 4], 6) returns [1,2]', () => {
11+
expect(twoSum([3, 2, 4], 6).sort((a, b) => (a < b ? -1 : 1))).toEqual([1, 2]);
12+
});
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// You are given two non-empty linked lists representing two non-negative
2+
// integers. The digits are stored in reverse order and each of their nodes
3+
// contain a single digit. Add the two numbers and return it as a linked list.
4+
// --- Example
5+
// Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
6+
// Output: 7 -> 0 -> 8
7+
// Explanation: 342 + 465 = 807.
8+
9+
class ListNode {
10+
constructor(val, next = null) {
11+
this.val = val;
12+
this.next = next;
13+
}
14+
}
15+
16+
function addTwoNumbers(l1, l2) {}
17+
18+
module.exports = addTwoNumbers;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const addTwoNumbers = require('./index');
2+
3+
class ListNode {
4+
constructor(val, next = null) {
5+
this.val = val;
6+
this.next = next;
7+
}
8+
}
9+
10+
const convertLLToArr = ll => {
11+
let arr = [];
12+
while (ll) {
13+
arr.push(ll.val);
14+
ll = ll.next;
15+
}
16+
return arr;
17+
};
18+
19+
const convertIntToLL = int => {
20+
let list = null;
21+
int.split('').forEach(digit => {
22+
const head = new ListNode(Number(digit), list);
23+
list = head;
24+
});
25+
return list;
26+
};
27+
28+
test('(2 -> 4 -> 3) + (5 -> 6 -> 4) = (7 -> 0 -> 8)', () => {
29+
// 342
30+
const l1 = new ListNode(2);
31+
l1.next = new ListNode(4);
32+
l1.next.next = new ListNode(3);
33+
34+
// 465
35+
const l2 = new ListNode(5);
36+
l2.next = new ListNode(6);
37+
l2.next.next = new ListNode(4);
38+
39+
// 342 + 465 = 807
40+
expect(convertLLToArr(addTwoNumbers(l1, l2))).toEqual([7, 0, 8]);
41+
});
42+
43+
test('(9) + (6) = (5 -> 1)', () => {
44+
// 342
45+
const l1 = new ListNode(9);
46+
47+
// 465
48+
const l2 = new ListNode(6);
49+
50+
// 342 + 465 = 807
51+
expect(convertLLToArr(addTwoNumbers(l1, l2))).toEqual([5, 1]);
52+
});
53+
54+
test('works for 10000000000000000000000001 + 465', () => {
55+
const l1 = convertIntToLL('10000000000000000000000001');
56+
const l2 = convertIntToLL('465');
57+
58+
expect(convertLLToArr(addTwoNumbers(l1, l2))).toEqual([
59+
6,
60+
6,
61+
4,
62+
0,
63+
0,
64+
0,
65+
0,
66+
0,
67+
0,
68+
0,
69+
0,
70+
0,
71+
0,
72+
0,
73+
0,
74+
0,
75+
0,
76+
0,
77+
0,
78+
0,
79+
0,
80+
0,
81+
0,
82+
0,
83+
0,
84+
1
85+
]);
86+
});

0 commit comments

Comments
 (0)