-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSumII.java
More file actions
20 lines (19 loc) · 597 Bytes
/
TwoSumII.java
File metadata and controls
20 lines (19 loc) · 597 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class TwoSumII {
public static int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length < 2) {
throw new IllegalArgumentException("No two sum solution");
}
int l = 0, r = nums.length - 1;
while (l < r) {
int sum = nums[l] + nums[r];
if (sum < target) {
l++;
} else if (sum > target) {
r--;
} else {
return new int[] {l + 1, r + 1};
}
}
throw new IllegalArgumentException("No two sum solution");
}
}