Skip to content

Commit 3cac6eb

Browse files
committed
JS: TwoSome from Leetcode
1 parent 81c8122 commit 3cac6eb

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

Programs/TwoSum.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// 1. Two Sum
2+
3+
// Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
4+
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
5+
// You can return the answer in any order.
6+
7+
8+
9+
// Example 1:
10+
// Input: nums = [2,7,11,15], target = 9
11+
// Output: [0,1]
12+
// Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
13+
14+
// Example 2:
15+
// Input: nums = [3,2,4], target = 6
16+
// Output: [1,2]
17+
18+
// Example 3:
19+
// Input: nums = [3,3], target = 6
20+
// Output: [0,1]
21+
22+
23+
24+
25+
26+
for (var i = 0; i < nums.length; i++) {
27+
28+
for (var n = 1; n < nums.length; n++) {
29+
30+
// console.log(nums[i] + nums[n])
31+
32+
if (nums[i] + nums[n] == target) {
33+
if (nums[i] == nums[n]) {
34+
35+
console.log([nums.indexOf(nums[i]), nums.indexOf(nums[i]) + 1])
36+
37+
} else {
38+
console.log([nums.indexOf(nums[i]), nums.indexOf(nums[n])])
39+
}
40+
41+
42+
43+
}
44+
45+
}
46+
47+
}

0 commit comments

Comments
 (0)