力扣-两数之和

题目描述:

 1 class Solution {
 2     public int[] twoSum(int[] nums, int target) {
 3         int[] indexs = new int[2];
 4         //创建hashmap
 5         HashMap<Integer,Integer> hash = new HashMap<Integer,Integer>();
 6         for(int i = 0; i < nums.length; i++){
 7             if(hash.containsKey(nums[i])){
 8                 indexs[0] = i;
 9                 indexs[1] = hash.get(nums[i]);
10                 return indexs;
11             }
12             // 两数查存入 ,value为下标
13             hash.put(target-nums[i],i);
14         }
15         return indexs;
16     }
17 }
原文地址:https://www.cnblogs.com/buhaohedeyouzicha/p/14430700.html