两数之和

    1. public static int[] twoSum(int[] nums, int target) {
    2. Map<Integer, Integer> map = new HashMap<>();
    3. int[] res = new int[2];
    4. for (int i = 0; i < nums.length; i++) {
    5. if (map.containsKey(target - nums[i])) {
    6. res[0] = i;
    7. res[1] = map.get(target - nums[i]);
    8. } else {
    9. map.put(nums[i], i);
    10. }
    11. }
    12. return res;
    13. }