이건 쉬웠당 15분도 안걸린듯
몇년전에도 풀었던거같다 유명한 문제자너 이름도 익숙하구
Question
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:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
My answer
어제도 썼던 indexOf 써서 풀었당
for문을 돌리면서 target에서 el을 뺀 diff값이 나머지 array에 있는지 확인하면 됨.
var twoSum = function(nums, target) { for(let i=0; i<nums.length; i++) { const foundIdx = nums.indexOf(target - nums[i]); if (foundIdx >= 0 && foundIdx !== i) { return [i, foundIdx] } } };
옛날에 푼걸 봐볼까
걍 똑같이 풀었네..
var twoSum = function(nums, target) { for (var i = 0; i <nums.length; i++) { var lastIdx = nums.lastIndexOf(target - nums[i]); if(lastIdx > 0) return new Array(i, lastIdx); } };
으 띄어쓰기 불편..
let i=0; i <nums.length
var i = 0; i<nums.length
i = 0, i < nums.length
or
i=0, i<nums.length
가 되면 편-안해질거같네여