Notice
Recent Posts
Recent Comments
Link
거의 알고리즘 일기장
[leetCode] - 1. Two Sum - 본문
problem
https://leetcode.com/problems/two-sum/
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.
solve
This problem is so simple and easy.
because, can use brute force!
code
function twoSum(nums: number[], target: number): number[] {
let result:number[]|null = null;
//for loop twice
for(let i=0; i<nums.length; i++){
//if result is not null, previous calcurate find result
if(result){
break;
}
const c1 = nums[i];
for(let j=i+1; j<nums.length; j++){
const c2 = nums[j];
//if c1, c2 is same target, this caclurate done
if(c1 + c2 === target){
result = [i, j];
break;
}
}
}
return result;
};
반응형
'알고리즘 문제풀이' 카테고리의 다른 글
[leetcode] - 217. Contains Duplicate (0) | 2023.04.01 |
---|---|
[leetcode] - 121. Best Time to Buy and Sell Stock (0) | 2023.03.31 |
백준 1175번 _ 배달 (2) | 2020.11.07 |
백준 1194번 _ 달이 차오른다, 가자. (0) | 2020.11.07 |
백준 5373번 _ 큐빙 (0) | 2020.11.07 |
Comments