Array:two sum

public static int[] twoSum(int[] nums, int target) {
        HashMap hashMap = new HashMap();
        int[] arr = new int[2];
        for (int i = 0; i < nums.length; i++) {
            if (!hashMap.containsKey(nums[i])) {
                hashMap.put(nums[i], i);
            } 
        }
        for (int i = 0; i < nums.length; i++) {
            if (hashMap.containsKey(target-nums[i])) {
                arr[0] = i;
                arr[1] = hashMap.get(target-nums[i]);
                if (arr[0]!=arr[1]) {
                    break;
                }
            }
        }
        return arr;
    }

你可能感兴趣的:(Array:two sum)