[문제 링크]
https://leetcode.com/problems/two-sum/description/
[문제]
정수 배열인 nums와 정수인 target을 준다.
배열 안에 두개의 합을 더해서 target이 나오는 두개의 인덱스를 i와 j라고 한다면
{i, j}를 반환해라.
[풀이 과정]
한쌍한쌍 모든 값을 검색해본다는 생각으로 접근해서 풀었습니다.
[코드]
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
for(int i = 0; i<nums.length; i++) {
for(int j = i+1; j<nums.length; j++) {
if(nums[i] + nums[j] == target) {
arr[0] = i;
arr[1] = j;
return arr;
}
}
}
return arr;
}
}
[회고]
쉬웠습니다.
질문과 피드백은 언제나 환영입니다.
감사합니다.
'CodingTest > LeetCode' 카테고리의 다른 글
[LeetCode] 503. Next Greater Element II (0) | 2025.03.13 |
---|---|
[LeetCode] 3184. Count Pairs That Form a Complete Day I (0) | 2025.03.13 |
[LeetCode] 2848. Points That Intersect With Cars (0) | 2025.03.12 |
[LeetCode] 20. Valid Parentheses (0) | 2025.03.12 |
[LeetCode] 2176. Count Equal and Divisible Paris in an Array (0) | 2025.03.10 |