본문 바로가기
Algorithm

[Algorithm][Array] 문제 풀이 #2 - Two Sum

by Lee David 2022. 7. 16.
반응형
문제 링크

- https://leetcode.com/problems/two-sum/

문제 풀이
public static int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> map = new HashMap<>();

    for (int i = 0; i < nums.length; i++) {
        int rest = target - nums[i];
        if(map.containsKey(rest))
            return new int[] {map.get(rest), i};
        map.put(nums[i], i);
    }

    return new int[] {};
}
테스트 케이스 통과 비율

아직은 이지 정도에서 더 갈고 닦아야 미디엄으로 올라갈 수 있을것 같다.

오늘 1시간 동안 미디엄 문제 풀다 테스트 케이스 50% 맞추고 이지부터 다시 시작해보려고 합니다.

반응형