Skip to content

Commit 0a3990a

Browse files
committed
Add a solution for two-sum
1 parent 2ed040b commit 0a3990a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

two-sum/dongzoolee.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function twoSum(nums: number[], target: number): number[] {
2+
const sz = nums.length;
3+
let l = 0,
4+
r = 1,
5+
sm = nums[0] + nums[1];
6+
7+
while (l <= sz - 2 && r <= sz - 1) {
8+
sm = nums[l] + nums[r];
9+
10+
if (sm === target) {
11+
return [l, r];
12+
}
13+
14+
if (l === r - 1) {
15+
(r++, (l = 0));
16+
} else {
17+
l++;
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)