LeetCode-1 Two Sum Solution (with Java)

1. Description: 

2.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-01-23
 3  */
 4 class Solution {
 5     public int[] twoSum(int[] nums, int target) {
 6         Map<Integer, Integer> map = new HashMap<Integer, Integer>();
 7         for (int i = 0; i < nums.length; i++) {
 8             if (map.containsKey(target - nums[i]))
 9                 return new int[] { map.get(target - nums[i]), i };
10             else
11                 map.put(nums[i], i);
12         }
13         return new int[] {-1, -1};
14     }
15 }

 

原文地址:https://www.cnblogs.com/sheepcore/p/12396327.html