-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTwo_Sum_Sol_03.java
More file actions
59 lines (40 loc) · 1.6 KB
/
Two_Sum_Sol_03.java
File metadata and controls
59 lines (40 loc) · 1.6 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 array.ex.two.sum;
import java.util.HashMap;
/*
출처: LeetCode, Two Sum
문제: Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
= 정수값이 담긴 어레이가 하나 있다. 그 배열 안에 있는 숫자 중에 숫자 2개를 더해서 특정한 값이 나오면 그 두 숫자의 어레이 인덱스를 반환해라.
배열 안에는 정답이 딱 2개만 있다. 같은 값을 쓸 수 없고 값은 2개의 인덱스를 반환해라.
ex) 2개의 값을 더해서 5가 나오는 배열 인덱스 2개를 반환해라.
|6|4|3|8|7|5|2|
-> 솔루션 3: Hash Table
-> 배열을 한 번 돌면서 hash table의 값을 key 담고, value는 인덱스를 담는다.
key|6|4|3|8|7|5|2|
val|0|1|2|3|4|5|6|
-> 포인터 1개를 선언해서 배열방을 돌면서 값을 1개씩 5에서 빼어서 (5 - 값 = 나머지 값 ) 의 나머지 값을 배열 방에 서 찾는다.
-> 담으면서 포인터도 돌리기
-> 시간복잡도: O(2n) -> O(n)
*/
public class Two_Sum_Sol_03 {
// nums: 데이터 배열
// target: 배열 값의 합
// twoSum 메서드
static int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer>map = new HashMap<Integer,Integer>();
// 검색하면서 해시테이블에 값을 저장한다.
for(int i= 0; i<nums.length; i++) {
if(map.containsKey(target-nums[i])) {
return new int[] {map.get(target-nums[i]), i};
}
// 해시맵에 값을 저장
map.put(nums[i], i);
}
throw new IllegalArgumentException("No two sum solution");
}
public static void main(String[] args) {
int[] nums = {6,4,3,8,7,5,2};
int[] result = twoSum(nums, 5);
System.out.println(result[0]+", "+result[1]);
}
}