Two Sum II - Input array is sorted Solutions in Java
Number 167
Difficulty Easy
Acceptance 54.1%
Link LeetCode
Solutions
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description//// Author : liuyubobobo/// Time : 2017-11-13// Brute Force// Time Complexity: O(n^2)// Space Complexity: O(1)public class Solution1 {public int[] twoSum(int[] numbers, int target) {if(numbers.length < 2 /*|| !isSorted(numbers)*/)throw new IllegalArgumentException("Illegal argument numbers");for(int i = 0 ; i < numbers.length ; i ++)for(int j = i+1 ; j < numbers.length ; j ++)if(numbers[i] + numbers[j] == target){int[] res = {i+1, j+1};return res;}throw new IllegalStateException("The input has no solution");}private boolean isSorted(int[] numbers){for(int i = 1 ; i < numbers.length ; i ++)if(numbers[i] < numbers[i-1])return false;return true;}private static void printArr(int[] nums){for(int num: nums)System.out.print(num + " ");System.out.println();}public static void main(String[] args) {int[] nums = {2, 7, 11, 15};int target = 9;printArr((new Solution1()).twoSum(nums, target));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description//// Author : liuyubobobo/// Time : 2017-11-13// Binary Search// Time Complexity: O(nlogn)// Space Complexity: O(1)public class Solution2 {public int[] twoSum(int[] numbers, int target) {if(numbers.length < 2 /*|| !isSorted(numbers)*/)throw new IllegalArgumentException("Illegal argument numbers");for(int i = 0 ; i < numbers.length - 1 ; i ++){int j = binarySearch(numbers, i+1, numbers.length-1, target - numbers[i]);if(j != -1){int[] res = {i+1, j+1};return res;}}throw new IllegalStateException("The input has no solution");}private int binarySearch(int[] nums, int l, int r, int target){if(l < 0 || l > nums.length)throw new IllegalArgumentException("l is out of bound");if(r < 0 || r > nums.length)throw new IllegalArgumentException("r is out of bound");while(l <= r){int mid = l + (r - l)/2;if(nums[mid] == target)return mid;if(target > nums[mid])l = mid + 1;elser = mid - 1;}return -1;}private boolean isSorted(int[] numbers){for(int i = 1 ; i < numbers.length ; i ++)if(numbers[i] < numbers[i-1])return false;return true;}private static void printArr(int[] nums){for(int num: nums)System.out.print(num + " ");System.out.println();}public static void main(String[] args) {int[] nums = {2, 7, 11, 15};int target = 9;printArr((new Solution2()).twoSum(nums, target));}}
Java solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/description//// Author : liuyubobobo/// Time : 2017-11-13// Two Pointers// Time Complexity: O(n)// Space Complexity: O(1)public class Solution3 {public int[] twoSum(int[] numbers, int target) {if(numbers.length < 2 /*|| !isSorted(numbers)*/)throw new IllegalArgumentException("Illegal argument numbers");int l = 0, r = numbers.length - 1;while(l < r){if(numbers[l] + numbers[r] == target){int[] res = {l+1, r+1};return res;}else if(numbers[l] + numbers[r] < target)l ++;else // numbers[l] + numbers[r] > targetr --;}throw new IllegalStateException("The input has no solution");}private boolean isSorted(int[] numbers){for(int i = 1 ; i < numbers.length ; i ++)if(numbers[i] < numbers[i-1])return false;return true;}private static void printArr(int[] nums){for(int num: nums)System.out.print(num + " ");System.out.println();}public static void main(String[] args) {int[] nums = {2, 7, 11, 15};int target = 9;printArr((new Solution3()).twoSum(nums, target));}}