Given an array of integers `nums` and an integer `target`, return the indices of the two numbers that add up to `target`. You may assume that each input has exactly one solution, and you may not use the same element twice.
Examples
Example 1 Input:nums = [2, 7, 11, 15], target = 9
Output:[0, 1]
Explanation: nums[0] + nums[1] = 2 + 7 = 9
Example 2 Input:nums = [3, 2, 4], target = 6
Output:[1, 2]
Constraints
▪2 <= nums.length <= 10^4
▪-10^9 <= nums[i] <= 10^9
▪Only one valid answer exists.
Hints
Hint 1Show
A brute force approach would check every pair — O(n²) time.
Hint 2Show
Can you reduce this to O(n) using extra space?
Hint 3Show
Use a hash map to store values you've seen and their indices.
Starter Code
Solution.java
1class Solution {
2 public int[] twoSum(int[] nums, int target) {
3 // Your code here
4 return new int[]{};
5 }
6}
Solution
Solve the problem first before reviewing the solution!