-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_0001TwoSum.java
77 lines (70 loc) · 2.38 KB
/
_0001TwoSum.java
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.heatwave.leetcode.problems;
import java.util.Arrays;
import java.util.HashMap;
/**
* 1. Two Sum
* Given an array of integers, return indices of the two numbers such that they add up to a specific target.
* <p>
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
* <p>
* Example:
* <p>
* Given nums = [2, 7, 11, 15], target = 9,
* <p>
* Because nums[0] + nums[1] = 2 + 7 = 9,
* return [0, 1].
*/
public class _0001TwoSum {
static class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = {0, 0};
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
res[0] = i;
res[1] = j;
return res;
}
}
}
return res;
}
}
static class SolutionHash {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
hashMap.put(nums[i], i);
}
for (int i = 0; i < nums.length; i++) {
int val = target - nums[i];
Integer valIndex = hashMap.get(val);
if (valIndex != null && valIndex != i) {
return new int[]{i, valIndex};
}
}
throw new IllegalArgumentException("no answer");
}
}
static class SolutionHashOnce {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int val = target - nums[i];
Integer valIndex = hashMap.get(val);
if (valIndex != null && valIndex != i) {
return new int[]{valIndex, i};
}
hashMap.put(nums[i], i);
}
throw new IllegalArgumentException("no answer");
}
}
public static void main(String[] args) {
Solution solution = new Solution();
int[] nums = {2, 7, 11, 15};
int target = 13;
int[] res = solution.twoSum(nums, target);
System.out.println(Arrays.toString(res));
}
}