← 返回 apple 的题目列表Two Sum Problem: Find Indices of Numbers Adding to Target
类型:online_judge
apple
Given an unsorted array of integers, find two numbers such that they add up to a specific target number. The solution must have a time complexity less than O(n^2).
Input
An unsorted array of integers, e.g., [2, 7, 11, 15]
An integer target value, e.g., 9
Output
Return the indices of the two numbers in the form of an array, e.g., [0, 1]
Note
Assume each input would have exactly one solution, and you may not use the same element twice.
Example
Input: nums = [2, 7, 11, 15], target = 9 Output: [0, 1]
Example
Input
4
2 7 11 15
9