자바스크립트로 백준 문제 풀기 도전!!
입력 형식이 너무 복잡하다..
요즘 좋다는 leet-code로 JavaScript 문제를 풀어보았다.
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.
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Input: nums = [3,2,4], target = 6
Output: [1,2]
Input: nums = [3,3], target = 6
Output: [0,1]
대충 배열에 있는 값 두개(인덱스 중복 불가)를 더해서 target이면 인덱스 두개를 리스트로 리턴하라는 소리
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
let number = []
for(let i = 0; i < nums.length - 1; i++) {
for(let j = i + 1; j < nums.length; j++) {
if(nums[i] + nums[j] == target) {
number = [i, j]
return number
}
}
}
};
이 쉬운 문제를 3번이나 틀렸다. ㅋㅋ
더 익숙해지면 많이 나아질듯!!