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 }

 

posted @ 2020-03-02 16:28  SheepCore  阅读(122)  评论(0编辑  收藏  举报