forked from xcv58/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
35 lines (31 loc) · 901 Bytes
/
Solution.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
31
32
33
34
35
import java.util.*;
public class Solution {
public int[] twoSum(int[] numbers, int target) {
int[] result = new int[2];
int[] tmpArray = numbers.clone();
Arrays.sort(tmpArray);
int num1 = 0;
int num2 = 0;
for (int i = 0, j = numbers.length - 1, sum; i < j;) {
sum = tmpArray[i] + tmpArray[j];
if (sum == target) {
num1 = tmpArray[i];
num2 = tmpArray[j];
break;
}
if (sum > target) {
j--;
} else {
i++;
}
}
for (int i = 0, j = 0; i < numbers.length && j < result.length; i++) {
int tmpInt = numbers[i];
if (tmpInt == num1 || tmpInt == num2) {
result[j] = i + 1;
j++;
}
}
return result;
}
}