Degree
Easy ★ (*´╰╯`๓)♬
Description:
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
1 | Given nums = [2, 7, 11, 15], target = 9, |
Ideas
- 获取数组中的第一个元素
- 找到除这个元素之外的其他元素,让它们相加,跟target进行对比
- 如果和target相等,就返回两个元素所在的index
- 重复1-3的步骤
Code
1 | var twoSum = function(nums, target) { |