一、题目
二、代码
class Solution
{
//map的定义 Map<Integer,Integer> map = new Hash<>();
//map 设置键值对 map.put(key,value);
//map 包含某个键 containsKey(key)
//map 通过键获取值 get(key)
public int[] twoSum(int[] nums, int target)
{
Map<Integer,Integer> map = new HashMap<>();
int i;
int length = nums.length;
for(i=0;i<length;i++)
{
int temp = target - nums[i];
if(map.containsKey(temp))
{
int[] re = new int[2];
re[0] = i;
re[1] = map.get(temp);
return re;
}
if(map.containsKey(nums[i])) continue;
else map.put(nums[i],i);
}
return new int[2];
}
}