-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum.java
More file actions
49 lines (44 loc) · 1.35 KB
/
Copy pathTwoSum.java
File metadata and controls
49 lines (44 loc) · 1.35 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
/*
* File Name:TwoSum is created on 2020/8/12 8:01 下午 by lite
*
* Copyright (c) 2020, xiaoyujiaoyu technology All Rights Reserved.
*
*/
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
/**
* @author lite
* @Description: 暴力;hash
* @date: 2020/8/12 8:01 下午
*/
public class TwoSum {
public static void main(String[] args) {
int[] nums = {2, 7, 11, 15};
int target = 9;
TwoSum test = new TwoSum();
System.out.println(Arrays.toString(test.violence(nums, target)));
System.out.println(Arrays.toString(test.hash(nums, target)));
}
public int[] violence(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
return new int[] {i, j};
}
}
}
throw new IllegalArgumentException("无解");
}
public int[] hash(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
int pair = target - nums[i];
if (map.containsKey(pair)) {
return new int[] {map.get(pair), i};
}
map.put(nums[i], i);
}
throw new IllegalArgumentException("无解");
}
}