본문 바로가기
개발공부/Javascript

#leetcode - Two Sum

by Sujin Agnes 2022. 1. 27.

 

 

Two Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

 

Example 1 :

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].

My Answer :

/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number[]}
 */

const twoSum = (nums, target) => {
   for(let i=0; i<nums.length; i++) {
       for(let j=i+1; j<nums.length; j++) {
           if(nums[i] + nums[j] === target) {
               return [i, j]
           }
       }
   } 
};

 

사수님이 스터디방을 만드시고 준 문제들 처음에 어버버 했지만 정신 부여잡고 하다가 별찍기 생각하며 해보았...다.

프로그래머스도 풀어봤지만 영어고 한글이고 문제보면 내가 해석을 못하나 싶고 주어진 조건이랑 example 보고 코드 작성하고 통과되고나서 다시 저 문장들을 다시 보아도....도저히.....무슨..말...? 

 

멀었다 멀었어 많이 멀었어 한~~참 멀었어....

틀린 부분이 있으시거나 더 좋은 방법이 있다면 둥글게둥글게 살포시 댓글로 알려주세요 :) 

상처 잘 받는 그런 아이랍니다...

 

'개발공부 > Javascript' 카테고리의 다른 글

#leetcode - Add Two Numbers  (0) 2022.01.27

댓글