两数之和
两数之和
https://www.nowcoder.com/practice/20ef0972485e41019e39543e8e895b7f?tpId=196&&tqId=37090&rp=1&ru=/ta/job-code-total&qru=/ta/job-code-total/question-ranking
//空间换时间
public int[] twoSum (int[] numbers, int target) { // write code here Map<Integer,Integer> map=new HashMap<>(); int[] res =new int[2]; for(int i=0;i<numbers.length;i++){ int cur=numbers[i]; if(map.containsKey(target-cur)){ res[0]=map.get(target-cur); res[1]=i+1; return res; }else map.put(cur,i+1); } return res; }