Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86fa4fe

Browse files
author
zongyanqi
committed
add Easy_1_Two_Sum.js Easy_167_Two_Sum_II_Input_array_is_sorted
1 parent9857a6b commit86fa4fe

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Lines changed: 29 additions & 0 deletions
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+
vartwoSum=function(numbers,target){
20+
21+
for(vari=0;i<numbers.length-1;i++){
22+
for(varj=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

Lines changed: 25 additions & 0 deletions
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+
vartwoSum=function(numbers,target){
19+
20+
for(vari=0;i<numbers.length-1;i++){
21+
for(varj=i+1;j<numbers.length;j++){
22+
if(numbers[i]+numbers[j]==target)return[i,j];
23+
}
24+
}
25+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp