Difficulty: Easy
規則
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:
1 | Input: nums = [2,7,11,15], target = 9 |
Example 2:
1 | Input: nums = [3,2,4], target = 6 |
Example 3:
1 | Input: nums = [3,3], target = 6 |
解題思路
首先會先得到一個數組和一個target數值,利用迴圈抓取數組內容,target數值減掉數組內容的值,所得到的差值就是我要去匹配其他數組內容是否符合,最終找到符合的回傳兩個值的引索位置。
Solution
Code 1 :
1 | var twoSum = function(nums, target) { |
Code 2 :
1 | var twoSum = function(nums, target) { |