Two Sum

来源:互联网 发布:java获取文件修改时间 编辑:程序博客网 时间:2024/04/27 00:10
public class Solution {    public int[] twoSum(int[] nums, int target) {        if (nums == null) {    throw new IllegalArgumentException("");    }    int [] res = new int[2];    if (nums.length < 2) {    return res;    }    Map<Integer, Integer> map = new HashMap<>();    for (int i = 0; i < nums.length; i++) {    if (map.containsKey(nums[i])) {    int idxa = map.get(nums[i]);    res[0] = idxa;    res[1] = i;    } else {    map.put(target - nums[i], i);    }    }    return res;    }}

0 0