-
Notifications
You must be signed in to change notification settings - Fork 15
/
2-sum.java
30 lines (28 loc) · 860 Bytes
/
2-sum.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
public class Solution {
/*
* @param numbers : An array of Integer
* @param target : target = numbers[index1] + numbers[index2]
* @return : [index1 + 1, index2 + 1] (index1 < index2)
*/
public int[] twoSum(int[] numbers, int target) {
// write your code here
int first = -1;
int second = -1;
boolean found = false;
for (int i = 0; i < numbers.length; i++) {
for (int j = numbers.length - 1; j > i; j--) {
if (numbers[i] + numbers[j] == target) {
first = i + 1;
second = j + 1;
found = true;
break;
}
}
if (found) {
break;
}
}
int[] res = {first, second};
return res;
}
}