[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;
    }
}
posted @ 2016-05-13 01:22  zhou23  阅读(96)  评论(0编辑  收藏  举报