-
[GeekforGeeks] Key Pair개인 공부/알고리즘 2021. 5. 30. 19:23
Key Pair
문제
n개의 양의 정수값을 가지는 배열에서 두 수의 합이 정확히 특정 숫자 x가 되는 원소가 존재하는지 여부를 반환하여라.
class Solution { boolean hasArrayTwoCandidates(int arr[], int n, int x) { // code here HashMap<Integer, Integer> pairs = new HashMap<>(); for (int i = 0; i < arr.length; i++) { if (pairs.containsValue(arr[i])) return true; else pairs.put(arr[i], x - arr[i]); } return false; } }
이제 이 문제는 아예 푸는 법을 외워버렸다...😅
'개인 공부 > 알고리즘' 카테고리의 다른 글
스레드 이진 트리 (Threaded Binary Tree) (0) 2021.10.10 [LeetCode] Climbing Stairs (0) 2021.05.18 [LeetCode] Flip Equivalent Binary Trees (0) 2021.05.16 [LeetCode] Valid Anagram (0) 2021.05.08 [Leetcode] Valid Parentheses (0) 2021.04.25