1. 两数之和(2019-01-12)

两数之和

class Solution {

    private Map map = new HashMap();

    public int[] twoSum(int[] nums, int target) {

        if (nums == null || nums.length < 2) {

            return null;

        }

        int i = 0;

        map.put(nums[i], i);

        for (i = 1; i < nums.length; i ++) {

            int number = nums[i];

            Object index = map.get(target - number);

            if (index != null) {

                return new int[] {(int) index, i};

            }

            map.put(number, i);

        }

        return null;

    }

}

你可能感兴趣的:(1. 两数之和(2019-01-12))