|
| 1 | +import java.util.HashMap; |
| 2 | +import java.util.Map; |
| 3 | + |
| 4 | +// tag renovizee 1week |
| 5 | +// https://github.com/DaleStudy/leetcode-study/issues/219 |
| 6 | +// https://leetcode.com/problems/two-sum/description/ |
| 7 | + |
| 8 | +// #์๊ตฌ์ฌํญ ์์ฝ |
| 9 | +// 1. int[] nums์ int target์ด ์ฃผ์ด์ง๋ค. |
| 10 | +// 2. nums์ ๋ ์์ ํฉ์ด target๊ณผ ๊ฐ์ int[] index๋ฅผ ๋ฆฌํดํ๋ค. (์์ ์๊ด x) |
| 11 | +// 3. ๋๊ฐ์ ์์๋ฅผ ๋๋ฒ ์ฌ์ฉํ์ง ๋ชปํ๊ณ , ์ ํํ ํ๋์ ์ ๋ต๋ง ์๋ค. |
| 12 | + |
| 13 | +class Solution { |
| 14 | + // Solv3: map ์ต์ ํ |
| 15 | + // ์๊ฐ๋ณต์ก๋ : O(n) |
| 16 | + // ๊ณต๊ฐ๋ณต์ก๋ : O(n) |
| 17 | + public int[] twoSum(int[] nums, int target) { |
| 18 | + Map<Integer, Integer> map = new HashMap<>(); |
| 19 | + int[] result = new int[2]; |
| 20 | + for (int i = 0; i < nums.length; i++) { |
| 21 | + int key = target - nums[i]; |
| 22 | + if (map.containsKey(key) && map.get(key) != i) { |
| 23 | + result[0] = i; |
| 24 | + result[1] = map.get(key); |
| 25 | + } |
| 26 | + map.put(nums[i], i); |
| 27 | + } |
| 28 | + return result; |
| 29 | + } |
| 30 | +//------------------------------------------------------------------------------------------------------------- |
| 31 | + // Solv2: map |
| 32 | + // ์๊ฐ๋ณต์ก๋ : O(n) |
| 33 | + // ๊ณต๊ฐ๋ณต์ก๋ : O(n) |
| 34 | +// public int[] twoSum(int[] nums, int target) { |
| 35 | +// Map<Integer, Integer> map = new HashMap<>(); |
| 36 | +// int[] result = new int[2]; |
| 37 | +// for (int i = 0; i < nums.length; i++) { |
| 38 | +// map.put(nums[i], i); |
| 39 | +// } |
| 40 | +// |
| 41 | +// for (int i = 0; i < nums.length; i++) { |
| 42 | +// int key = target - nums[i]; |
| 43 | +// if (map.containsKey(key) && map.get(key) != i) { |
| 44 | +// result[0] = i; |
| 45 | +// result[1] = map.get(key); |
| 46 | +// } |
| 47 | +// } |
| 48 | +// return result; |
| 49 | +// |
| 50 | +// } |
| 51 | +//------------------------------------------------------------------------------------------------------------- |
| 52 | +// Solv1: Brute Force |
| 53 | +// ์๊ฐ๋ณต์ก๋ : O(n^2) |
| 54 | +// ๊ณต๊ฐ๋ณต์ก๋ : O(1) |
| 55 | +// public int[] twoSum(int[] nums, int target) { |
| 56 | +// int size = nums.length; |
| 57 | +// for(int i = 0; i < size - 1; i++) { |
| 58 | +// for(int j = i+1; j < size; j++) { |
| 59 | +// if(target == (nums[i] + nums[j])){ |
| 60 | +// return new int[]{i,j}; |
| 61 | +// } |
| 62 | +// } |
| 63 | +// } |
| 64 | +// return new int[]{}; |
| 65 | +// } |
| 66 | + |
| 67 | + |
| 68 | +// 1) ==: ๋ ๊ฐ์ด ๊ฐ์์ง ๋น๊ต. ๊ธฐ๋ณธ ํ์
์ ๊ฐ์ ๋น๊ตํ๊ณ , ์ฐธ์กฐ ํ์
์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์(๋์ผํ ๊ฐ์ฒด์ธ์ง)๋ฅผ ๋น๊ต |
| 69 | +// ์ฐธ์กฐ ํ์
๊ฐ์ฒด์ ๋ด์ฉ์ด ๊ฐ์์ง๋ฅผ ๋น๊ตํ๋ ค๋ฉด ์ฃผ๋ก a.equals(b)๋ฅผ ์ฌ์ฉ |
| 70 | +// |
| 71 | +// 2) ์ด๊ธฐํ ๋ฐฐ์ด๊ณผ ๋งต |
| 72 | +// - new int[2] :size ์ด๊ธฐํ |
| 73 | +// - new int[]{1,2,3} : ์ค์ ๊ฐ ์ด๊ธฐํ |
| 74 | +// - Map<String,String> test = new HashMap<>(); ๋งต์ k/v ํ์
์ ์ ๋ณ์์ ์ค์ ํ๋ค. val ๋ง์ฌ์ฉํ๋ค.. |
| 75 | +//------------------------------------------------------------------------------------------------------------- |
| 76 | + |
| 77 | +} |
0 commit comments