[LeetCode] 1. Two Sum

이지현·2023년 9월 13일
0

Algorithm

목록 보기
80/81
post-thumbnail

✔️ Problem URL

문자열 겹쳐쓰기


✔️ Problem

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.


✔️ Code

class Solution {
    public int[] twoSum(int[] nums, int target) {
        int[] answer = new int[2];

        if(nums.length == 2) {
            answer[0] = 0;
            answer[1] = 1;
        }
        for(int i = 0; i < nums.length; i++) {
            for(int j = i+1; j < nums.length; j++) {
                if((nums[i] + nums[j]) == target) {
                    answer[0] = i;
                    answer[1] = j;
                }
            }
        }
        return answer;
    }
}

profile
2023.09 ~ 티스토리 이전 / 2024.04 ~ 깃허브 블로그 이전

0개의 댓글