-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.java
More file actions
38 lines (35 loc) · 969 Bytes
/
Solution.java
File metadata and controls
38 lines (35 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.q0001_two_sum;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author xjn
* @since 2020-02-15
* https://leetcode-cn.com/problems/two-sum/
* 1. 两数之和
*
* 时间复杂度O(n)
* 空间复杂度O(n)
*/
public class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return null;
}
//k:value
//v:index
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(target - nums[i])) {
return new int[]{i, map.get(target - nums[i])};
}
map.put(nums[i], i);
}
return null;
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] ints = solution.twoSum(new int[]{2, 7, 11, 15}, 9);
System.out.println(Arrays.toString(ints));
}
}