-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy path056. 两数之和.java
58 lines (58 loc) · 1.54 KB
/
056. 两数之和.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import java.util.Collections;
import java.util.ArrayList;
import java.util.TreeMap;
public class Solution
{
public int[] twoSum(int[] numbers, int target)
{
ArrayList<Pair> list = new ArrayList<Pair>();
int[] result =new int[2];
for(int i=0;i<numbers.length;++i)
{
list.add(new Pair(i,numbers[i]));
}
Collections.sort(list);
int min=0;
int max=numbers.length-1;
while(min<max)
{
if(list.get(min).value+list.get(max).value==target)
{
if(list.get(min).yuan_lai<list.get(max).yuan_lai)
{
result[0]=list.get(min).yuan_lai+1;
result[1]=list.get(max).yuan_lai+1;
}
else
{
result[0]=list.get(max).yuan_lai+1;
result[1]=list.get(min).yuan_lai+1;
}
break;
}
else if(list.get(min).value+list.get(max).value<target)
{
++min;
}
else
{
--max;
}
}
return result;
}
class Pair implements Comparable
{
int yuan_lai;
int value;
public Pair(int a,int b)
{
yuan_lai = a;
value = b;
}
public int compareTo(Object other)
{
return Integer.valueOf(value).compareTo(Integer.valueOf(((Pair)other).value));
}
}
}