LC刷题-1

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
		Map map = new HashMap<>();
		for (int i = 0; i < nums.length; i++) {
			map.put(nums[i], i);
		}
		for (int i = 0; i < nums.length; i++) {
			Integer x = map.get(target - nums[i]);
			if (null != x && x != i) {
				res[0] = i;
				res[1] = x;
			}
		}
		return res;
    }
}

你可能感兴趣的:(LC刷题-1)