Two Sum
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
1 public int adjust_array(int[] input,int low,int high){ 2 //set the pivot to be the first element of the array 3 int pivot = input[low]; 4 int exchange = 0; 5 while(low < high){ 6 //from the tail to header,find the item smaller than the pivot 7 while(input[high] >= pivot && high > low) 8 high --; 9 exchange = input[high]; 10 input[high] = input[low]; 11 input[low] = exchange; 12 //from the header to tail,find the item larger than the pivot 13 while(input[low] <= pivot && high > low) 14 low ++; 15 exchange = input[high]; 16 input[high] = input[low]; 17 input[low] = exchange; 18 } 19 //set the mid to be pivot 20 input[low] = pivot; 21 22 return low; 23 } 24 25 public void quick_sort(int[] input, int low, int high){ 26 //set the low and high pointer 27 if(low < high){ 28 int mid = adjust_array(input,low,high); 29 quick_sort(input, 0, mid - 1); 30 quick_sort(input, mid + 1, high); 31 } 32 } 33 34 public void pro4(){ 35 int[] input = {20,1,2,40,7,90,11}; 36 int low = 0; 37 int high = input.length - 1; 38 quick_sort(input,low,high); 39 for(int i = 0; i < input.length; i ++){ 40 System.out.println(input[i]); 41 } 42 } 43 44 public int[] twoSum(int[] input, int target){ 45 //sort the array 46 int low = 0; 47 int high = input.length - 1; 48 quick_sort(input,low,high); 49 int[] returns = new int[2]; 50 51 while(input[high] > target){ 52 high --; 53 } 54 for(int i = high; i > 0; i ++){ 55 for(int j = low; j < high; j ++){ 56 int sum = input[i] + input[j]; 57 if(sum == target){ 58 returns[0] = input[j]; 59 returns[1] = input[i]; 60 return returns; 61 } 62 if(sum > target) 63 break; 64 } 65 } 66 return returns; 67 } 68 69 public void pro5(){ 70 int[] input ={20,1,2,40,7,90,11}; 71 int target = 61; 72 int[] out = twoSum(input,target); 73 System.out.println("first: "+ out[0]); 74 System.out.println("second: "+ out[1]); 75 }
To improve the time complexity.
1 public int[] twoSum(int[] input, int target){ 2 //sort the array 3 int low = 0; 4 int high = input.length - 1; 5 quick_sort(input,low,high); 6 int[] returns = new int[2]; 7 8 while(input[high] > target){ 9 high --; 10 } 11 // for(int i = high; i > 0; i ++){ 12 // for(int j = low; j < high; j ++){ 13 // int sum = input[i] + input[j]; 14 // if(sum == target){ 15 // returns[0] = input[j]; 16 // returns[1] = input[i]; 17 // return returns; 18 // } 19 // if(sum > target) 20 // break; 21 // } 22 // } 23 int sum = 0; 24 while(high > low){ 25 sum = input[low] + input[high]; 26 if(sum > target) high --; 27 else if(sum < target) low ++; 28 else{ 29 returns[0] = input[low]; 30 returns[1] = input[high]; 31 return returns; 32 } 33 } 34 returns[0] = returns[1] = -1; 35 return returns; 36 37 }
posted on 2013-03-26 10:57 Step-BY-Step 阅读(234) 评论(0) 编辑 收藏 举报