-
[leetcode] 1. Two Sum (Typescript)Algorithm 2021. 2. 12. 14:25728x90
문제
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]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
function twoSum(nums: number[], target: number): number[] { let result = []; for (let i = 0; i < nums.length - 1; i++) { for (let j = i + 1; j < nums.length; j++) { if (nums[i] + nums[j] === target) { result = [i, j]; } } } if (result.length > 0) { return result; } }; twoSum([2,7,11,15], 9);
728x90'Algorithm' 카테고리의 다른 글
[Algorithm] Sliding Window 알고리즘 (Javascript) (0) 2021.02.14 [leetcode] 2. Add two numbers (Typescript) (0) 2021.02.13 [백준] 11653번 소인수분해 (Node.js) (0) 2021.01.23 에라토스테네스의 체 (0) 2021.01.23 [백준] 10757 큰 수 A + B - NodeJS (0) 2021.01.17