Question Description
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
method 1
之前看《算法导论》看到过这个题,想法就是先归并排序,再二分查找。但是问题要求返回index,而代码是基于排序后的数组进行查找的,这样就要copy出一个原始数组,进行查找index,这样觉得效果就不好了。【这块能否提升?】O(nlogn)
runtime
java
public class Solution { //mergeSort public void mergeSort(int[] numbers,int begin,int end){ int mid= 0; if(begin<end){ mid = (begin+end)/2; mergeSort(numbers,begin,mid); mergeSort(numbers,mid+1,end); merge(numbers,begin,mid,end); } } public int[] assignlist(int[]array, int startnum, int endnum){ int assigned[] = new int[endnum - startnum + 2]; int k=0; for(int i = startnum; i<=endnum; i++){ assigned[k] = array[i]; k++; } assigned[k] = Integer.MAX_VALUE; return assigned; } public void merge(int[] numbers,int begin,int mid, int end){ int[] right = assignlist(numbers,begin,mid); int[] left = assignlist(numbers,mid+1,end); int i=0; int j=0; for(int k=begin;k<=end;k++){ if(left[i]<=right[j]){ numbers[k] = left[i]; //System.out.println(""+numbers[k]); i++; } else{ numbers[k] = right[j]; j++; } } } //binarySort return index if found public int binarySort(int[] numbers,int searchnum,int begin,int end){ int flag = -1; while(begin<=end){ int mid = (begin+end)/2; if(searchnum<numbers[mid]){ end = mid-1; } else if(searchnum>numbers[mid]){ begin=mid+1; } else if(searchnum==numbers[mid]){ flag = mid; return flag; } } return flag; } public int[] twoSum(int[] numbers, int target) { int result[] = new int[2]; int result2[] = new int[2]; int numberscopy[] = new int[numbers.length]; for(int i=0;i<numbers.length;i++){ numberscopy[i] = numbers[i]; } mergeSort(numbers,0,numbers.length-1); for(int i=0;i<numbers.length;i++){ int flag=-1;//0 : not found; 1 found; flag = binarySort(numbers,target-numbers[i],i+1,numbers.length-1); if(flag != -1){ result[0] = i; result[1] = flag; } } for(int i=0;i<result.length;i++){ System.out.println("after sort: "+result[i]); } for(int i=0;i<numberscopy.length;i++){ if(numbers[result[0]] == numberscopy[i]){ result2[0] = i+1; } } for(int i=0;i<numberscopy.length;i++){ if(numbers[result[1]] == numberscopy[i]){ if(i+1 != result2[0]) result2[1] = i+1; } } if(result2[0]>result2[1]){ int temp; temp = result2[0]; result2[0] = result2[1]; result2[1] = temp; } return result2; } }
method 2
利用hashmap,将每个元素值作为key,数组索引作为value存入hashmap,然后遍历数组元素,在hashmap中寻找与之和为target的元素。O(n)
runtime, O(n) space
– Hash table:
觉得第二种方法更加高效,而且beautiful!
java
public class Solutions2 { public int[] twoSum(int[] numbers, int target) { int len = numbers.length; int result[] = new int[2]; Map<Integer,Integer> map = new HashMap<Integer,Integer>(); for(int i = 0;i<len;i++){ map.put( numbers[i],i+1); } for(int i=0;i<len;i++){ Integer two = map.get(target-numbers[i]); if(two != null && i<two){ result[0] = i+1; result[1] = two; } } return result; }
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。