1、给定一个数组,找到数组中两个元素等于一个值。
1.1、先排序,两个指针,一个指头一个指尾,遍历一遍即可找到,时间复杂度为O(nlgn)(排序的时间),空间复杂度为O(1)。
1.2、利用hashmap存放当前元素对应的元素,遍历一次就可找到,时间复杂度为O(n),空间复杂度为O(n)。
public static int[] getTwoSum(int []val,int target){
HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
int result[]=new int[2];
for(int i=0;i<val.length;i++){