Practice/Meta/Leetcode 1. Two Sum
CodingMust
You are given an array of integers and a target integer value. Your task is to locate two different elements in the array that add up to exactly the target value, then return their positions in the array.
You may assume that every input will have exactly one valid answer, and you cannot use the same array element twice (the two indices must be different).
Example 1:
Input: nums = [2, 7, 11, 15], target = 9 Output: [0, 1] Explanation: The values at index 0 and index 1 are 2 and 7, which sum to 9.
Example 2:
Input: nums = [3, 2, 4], target = 6 Output: [1, 2] Explanation: The values at index 1 and index 2 are 2 and 4, which sum to 6.
Example 3:
Input: nums = [3, 3], target = 6 Output: [0, 1] Explanation: Both elements have the value 3, and they sum to 6. We return their different indices.