Difficulty: EASY
Categories: ๋ฐฐ์ด/๋ฌธ์์ดํด์/๋งต
class Solution {
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 new int[0];
}
}
[์๋ ํ์ด ยท gpt-4.1-mini] ํด์๋งต์ ์ด์ฉํด ๊ฐ ์ซ์์ ์ธ๋ฑ์ค๋ฅผ ์ ์ฅํ๋ฉฐ, ํ์ฌ ์ซ์์ ๋ํด target์ด ๋๋ ์ซ์๊ฐ ํด์๋งต์ ์๋์ง ํ์ธํ๋ค. ์์ผ๋ฉด ๋ฐ๋ก ์ธ๋ฑ์ค ์์ ๋ฐํํ๋ค. ์๊ฐ O(n) ยท ๊ณต๊ฐ O(n) ์ฃผ์: ๊ฐ์ ์ซ์๋ฅผ ๋ ๋ฒ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ๋ฅผ ์ฃผ์ํด์ผ ํ๋ฉฐ, ์ธ๋ฑ์ค ์ ์ฅ ์ ํ์ฌ ์ธ๋ฑ์ค๋ก ๋ฎ์ด์ฐ์ง ์๋๋ก ํ๋ค.