给出一个整型数组 numbers 和一个目标值 target,请在数组中找出两个加起来等于目标值的数的下标,返回的下标按升序排列。
(注:返回的数组下标从1开始算起,保证target一定可以由数组里面2个数字相加得到)
数据范围:,,
要求:空间复杂度 ,时间复杂度
[3,2,4],6
[2,3]
因为 2+4=6 ,而 2的下标为2 , 4的下标为3 ,又因为 下标2 < 下标3 ,所以返回[2,3]
[20,70,110,150],90
[1,2]
20+70=90
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < numbers.length; i++) { if (map.containsKey(target - numbers[i])) { return new int[] {map.get(target - numbers[i]) + 1, i + 1}; } else { map.put(numbers[i], i); } } throw new IllegalArgumentException("no solution"); } }
int[] array = new int[]{2,4,7,8,21,14,17,18}; int target = 39; int[] result = new int[2]; boolean flag = false; for (int i=0; i<array.length; i++){ int temp = target - array[i]; if (flag) break; for(int j=0; j<array.length; j++){ if(i != j && temp == array[j]){ result[0] = i+1; result[1] = j+1; flag = true; break; } } } System.out.println(JSONObject.toJSON(result));
public int[] twoSum (int[] numbers, int target) { //因为返回的是下标 int[] ans=new int[2]; if(numbers==null||numbers.length==0) return ans; for(int i=0;i<numbers.length;i++){ //截止条件 if(map.containsKey(target-numbers[i])){ //因为数组下标从1算起,所以这里的值都要+1 ans[0]=Math.min(i+1,map.get(target-numbers[i])+1); ans[1]=Math.max(i+1,map.get(target-numbers[i])+1); break; } map.put(numbers[i],i); } return ans; }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here // 先对数组进行排序,利用二分法查找 int[] sort = numbers.clone(); Arrays.sort(sort); // 中间索引 int medIndex = 0; // 数组长度 int len = sort.length; for (int i = 0; i < len; i++) { int left = i + 1; int right = len - 1; while (left <= right) { // 与中间元素比较 medIndex = (right + left) / 2; if (sort[i] + sort[medIndex] == target) { return find(numbers, sort[i], sort[medIndex]); } if (sort[i] + sort[medIndex] < target) { left = medIndex + 1; continue; } if (sort[i] + sort[medIndex] > target) { right = medIndex - 1; } } } return null; } public int[] find(int[] arr, int a, int b) { int[] result = new int[2]; int index1 = 0; int index2 = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] == a) { index1 = i; break; } } for (int i = 0; i < arr.length; i++) { if (arr[i] == b && i != index1) { index2 = i; break; } } if (index1 < index2) { result[0] = index1 + 1; result[1] = index2 + 1; } else { result[0] = index2 + 1; result[1] = index1 + 1; } return result; } }
题目要求复杂度是O(nlogn),那么显而易见就是一次循环加每个循环里一个二分查找。
public int[] twoSum (int[] numbers, int target) { //hashmap int nums = numbers.length; Map<Integer,Integer> map =new HashMap<>(); for (int i = 0; i < nums ; i++) { if(map.containsKey(target - numbers[i])){ // map 包含 返回 // 第i个找到 所以后面是i+1 前面的通过map get value return new int[]{(map.get(target - numbers[i]))+1, i+1}; }else{ //是把自己放进去 map.put(numbers[i],i); } } return new int[]{-1,-1}; }
import java.util.*; public class Solution { /** * 不使用HashMap的一种方法,运行时间364ms,占用内存29140KB * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here int length = numbers.length; int[] tool = Arrays.copyOf(numbers, length); Arrays.sort(tool); int[] result = new int[2]; for(int i=0,j=length-1;i<j;){ if(tool[i]+tool[j]<target){ i++; }else if(tool[i]+tool[j]>target){ j--; }else{ result[0]=tool[i]; result[1]=tool[j]; break; } } int index1 = -1 ; int index2 = -1 ; for(int k=0;k<length;k++){ if(numbers[k] == result[0] && index1==-1){ index1=k; }else if (numbers[k]==result[1] && index2==-1){ index2=k; } } if(index1<index2){ result[0]=index1+1; result[1]=index2+1; }else { result[0]=index2+1; result[1]=index1+1; } return result; } }
public class Solution { /** * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here Map<Integer,Integer> map = new HashMap<>(); for (int i = 0; i < numbers.length; i++) { if (map.containsKey(target - numbers[i])) { return new int[]{map.get(target - numbers[i])+1,i +1}; }else { map.put(numbers[i],i); } } return null; } }
import java.util.*; public class Solution { private Map<Integer,Integer> map = new HashMap<>(); /** * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here if (numbers == null || numbers.length <= 0) { return new int[0]; } for(int i = 0;i<numbers.length;i++) { if(map.containsKey(target - numbers[i])) { return new int[]{map.get(target - numbers[i]), i+1}; }else { map.put(numbers[i], i+1); } } return new int[0]; } }
import java.util.*; public class Solution { /** * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { int[] index = new int[2]; HashMap<Integer,Integer> map = new HashMap<>(); for(int i = 0; i < numbers.length; i++){ if(map.containsKey(target - numbers[i])){ index[0] = map.get(target - numbers[i]); index[1] = i + 1; } map.put(numbers[i],i+1); } return index; } }
import java.util.*; public class Solution { /** * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here int[] r = new int[2]; int j = 0; Map map = new HashMap<>(); for (int i = 0; i < numbers.length; i++) { if (!map.containsKey(target - numbers[i])) { map.put(numbers[i], i + 1); } else { r[j++] = (int)map.get(target - numbers[i]); r[j] = i + 1; } } Arrays.sort(r); return r; } }
import java.util.*; public class Solution { /** * * @param numbers int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] numbers, int target) { // write code here int[] result=new int[2]; for(int i=1;i<=numbers.length-1;i++){ for(int j=i+1;j<=numbers.length;j++){ if(numbers[i-1]+numbers[j-1]==target){ result[0]=i; result[1]=j; break; } } } return result; } }
int[] ints = new int[2]; Map<Integer,Integer> map = new HashMap(); for (int i = 0; i < numbers.length; i++) { Integer nn = numbers[i]; //第一次判断差值里是否有该值 if (map.containsKey(nn)) { ints[0] = map.get(nn) + 1; ints[1] = i + 1; return ints; } int cha = target - nn; map.put(cha,i);}用Map就能过,用list装就超时