Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 86fa4fe

Browse files
author
zongyanqi
committed
add Easy_1_Two_Sum.js Easy_167_Two_Sum_II_Input_array_is_sorted
1 parent 9857a6b commit 86fa4fe

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
3+
4+
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
5+
6+
You may assume that each input would have exactly one solution and you may not use the same element twice.
7+
8+
Input: numbers=[2, 7, 11, 15], target=9
9+
Output: index1=1, index2=2
10+
11+
12+
*/
13+
14+
/**
15+
* @param {number[]} numbers
16+
* @param {number} target
17+
* @return {number[]}
18+
*/
19+
var twoSum = function (numbers, target) {
20+
21+
for (var i = 0; i < numbers.length - 1; i++) {
22+
for (var j = i + 1; j < numbers.length; j++) {
23+
if (numbers[i] + numbers[j] == target) return [i + 1, j + 1];
24+
}
25+
}
26+
};
27+
28+
29+
console.log(twoSum([2, 7, 11, 15], 9))

Easy_1_Two_Sum.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
3+
4+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
5+
6+
Example:
7+
Given nums = [2, 7, 11, 15], target = 9,
8+
9+
Because nums[0] + nums[1] = 2 + 7 = 9,
10+
return [0, 1].
11+
*/
12+
13+
/**
14+
* @param {number[]} numbers
15+
* @param {number} target
16+
* @return {number[]}
17+
*/
18+
var twoSum = function (numbers, target) {
19+
20+
for (var i = 0; i < numbers.length - 1; i++) {
21+
for (var j = i + 1; j < numbers.length; j++) {
22+
if (numbers[i] + numbers[j] == target) return [i, j];
23+
}
24+
}
25+
};

0 commit comments

Comments
 (0)