Created
June 19, 2019 21:03
-
-
Save restart916/9eab4ef7f9605e2a7f45a7831f676260 to your computer and use it in GitHub Desktop.
20190619_leetcode_1_TwoSum
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {number[]} nums | |
* @param {number} target | |
* @return {number[]} | |
*/ | |
var twoSum = function(nums, target) { | |
for (let i in nums) { | |
let first = nums[i] | |
let searchVal = target - first; | |
let secondIndex = nums.findIndex((v) => {return v == searchVal}) | |
if (secondIndex != -1 && secondIndex != i) { | |
return [i, secondIndex] | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
두개의 합이 target 이 되는걸 찾기 (a+b = t)
-> 한 원소의 값을 target에서 빼면 찾고 싶은 값 (b = t-a)
해서 전체 원소를 돌면서 단순하게 찾은 로직입니다.
O(n제곱) .. 일까요?