1. Two Sum

    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;



    }

你可能感兴趣的:(1. Two Sum)