forked from lilong-dream/LeetCode-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path001TwoSum.java
More file actions
81 lines (68 loc) · 2.33 KB
/
001TwoSum.java
File metadata and controls
81 lines (68 loc) · 2.33 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Author: Li Long, [email protected]
// Date: Apr 17, 2014
// Source: http://oj.leetcode.com/problems/two-sum/
// Analysis: http://blog.csdn.net/lilong_dream/article/details/19298357
// Given an array of integers, find two numbers such that they add up to a specific target number.
// 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.
// You may assume that each input would have exactly one solution.
// Input: numbers={2, 7, 11, 15}, target=9
// Output: index1=1, index2=2
import java.util.ArrayList;
import java.util.Arrays;
public class TwoSum {
public int[] twoSum(int[] numbers, int target) {
// Note: The Solution object is instantiated only once and is reused by each test case.
int[] num = numbers.clone();
Arrays.sort(num);
int length = numbers.length;
int left = 0;
int right = length - 1;
int sum = 0;
ArrayList<Integer> index = new ArrayList<Integer>();
while(left < right)
{
sum = num[left] + num[right];
if(sum == target)
{
for(int i = 0; i < length; ++i)
{
if(numbers[i] == num[left])
{
index.add(i+1);
}
else if(numbers[i] == num[right])
{
index.add(i+1);
}
if(index.size() == 2)
{
break;
}
}
break;
}
else if(sum > target)
{
--right;
}
else
{
++left;
}
}
int[] result = new int[2];
result[0] = index.get(0);
result[1] = index.get(1);
return result;
}
public static void main(String[] args)
{
TwoSum slt = new TwoSum();
int[] numbers = {0, 4, 3, 0};
int[] index = new int[2];
index = slt.twoSum(numbers, 0);
System.out.println("index1 = " + index[0] + ", index2 = " + index[1]);
}
}