-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum.java
44 lines (39 loc) · 1.14 KB
/
TwoSum.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
import java.util.Arrays;
class Solution {
public int[] twoSum(int[] nums, int target) {
// int[] result = new int[2];
// int left = 0;
// int right = 1;
// int size = nums.length - 1;
// while ( left < right) {
// if ( nums[left] + nums[right] == target ) {
// result[0] = left;
// result[1] = right;
// left = size;
// } else if ( right == size) {
// left++;
// right = left+1;
// } else {
// right++;
// }
// }
// return result;
// }
for ( int left = 0; left < nums.length-1; left++) {
for (int right = left + 1; right < nums.length; right++) {
if ( nums[left] + nums[right] == target) {
return new int[] {left, right};
}
}
}
return null;
}
}
public class TwoSum {
public static void main(String[] args ) {
int[] nums = {3,2,4};
int target = 6;
Solution sum = new Solution();
sum.twoSum(nums, target);
}
}