[Leetcode] 001.Two Sum
地址:https://leetcode.com/problems/two-sum/
本题使用HashMap来处理
public class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
int defaultResult = {0,0};
int len = nums.length;
for(int i = 0;i<len;i++){
if(map.get(target - nums[i])!=null){
int result = {map.get(target - nums[i]),i};
return result;
}
map.put(nums[i],i);
}
return defaultResult;
}
}