Skip to content

Instantly share code, notes, and snippets.

@restart916
Created June 19, 2019 21:03
Show Gist options
  • Save restart916/9eab4ef7f9605e2a7f45a7831f676260 to your computer and use it in GitHub Desktop.
Save restart916/9eab4ef7f9605e2a7f45a7831f676260 to your computer and use it in GitHub Desktop.
20190619_leetcode_1_TwoSum
/**
* @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]
}
}
};
@restart916
Copy link
Author

두개의 합이 target 이 되는걸 찾기 (a+b = t)
-> 한 원소의 값을 target에서 빼면 찾고 싶은 값 (b = t-a)

해서 전체 원소를 돌면서 단순하게 찾은 로직입니다.
O(n제곱) .. 일까요?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment