Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Solution {
- /*
- * @param nums an array of Integer
- * @param target = nums[index1] + nums[index2]
- * @return [index1 + 1, index2 + 1] (index1 < index2)
- */
- public int[] twoSum(int[] nums, int target) {
- if (nums == null || nums.length < 2) {
- return null;
- }
- int start = 0, end = nums.length - 1;
- while (start < end) {
- if (nums[start] + nums[end] == target) {
- int[] pair = new int[2];
- pair[0] = start + 1;
- pair[1] = end + 1;
- return pair;
- }
- if (nums[start] + nums[end] < target) {
- start++;
- } else {
- end--;
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement