-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP0001_TwoSum.java
More file actions
59 lines (54 loc) · 2.09 KB
/
P0001_TwoSum.java
File metadata and controls
59 lines (54 loc) · 2.09 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package yyl.leetcode.p00;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* <h3>两数之和</h3><br>
* 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。<br>
* 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。<br>
* 示例:<br>
* 给定 nums = [2, 7, 11, 15], target = 9<br>
* 因为 nums[0] + nums[1] = 2 + 7 = 9<br>
* 所以返回 [0, 1]<br>
* <br>
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.<br>
* You may assume that each input would have exactly one solution, and you may not use the same element twice.<br>
*/
public class P0001_TwoSum {
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = { 2, 7, 11, 15 };
int target = 9;
int[] result = solution.twoSum(nums, target);
System.out.println(Arrays.toString(result));
}
// Time complexity : O(n^2)
// Space complexity : O(1)
static class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] + nums[i] == target) {
return new int[] { i, j };
}
}
}
return null;
}
}
// Time complexity : O(n)
// Space complexity : O(n)
static class Solution2 {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int complement = target - nums[i];
if (map.containsKey(complement)) {
return new int[] { map.get(complement), i };
}
map.put(nums[i], i);
}
return null;
}
}
}