• 문제 링크
Two Sum - LeetCode
Can you solve this real interview question? Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not
leetcode.com
• 풀이 코드
import java.util.HashMap;
import java.util.Map;
public class Solution {
public int[] twoSum(int[] nums, int target) {
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; ++i) {
Integer v = map.get(nums[i]);
if (map.get(nums[i]) != null) return new int[]{v, i};
map.put(target - nums[i], i);
}
return null;
}
}'Problem Solving > LeetCode' 카테고리의 다른 글
| [LeetCode] 12. Integer to Roman - Java (0) | 2026.05.09 |
|---|---|
| [LeetCode] 11. Container With Most Water - Java (0) | 2026.05.08 |
| [LeetCode] 9. Palindrome Number - Java (0) | 2026.05.07 |
| [LeetCode] 3. Longest Substring Without Repeating Characters - Java (0) | 2026.05.06 |
| [LeetCode] 2. Add Two Numbers - Java (0) | 2026.05.05 |
댓글