-
[LeetCode] 1346. Check If N and Its Double ExistLeetCode 2021. 11. 4. 12:02728x90
https://leetcode.com/problems/check-if-n-and-its-double-exist/
Check If N and Its Double Exist - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Check If N and Its Double Exist - Easy

배열 내의 어떤수가 다른 어떤수의 두배가 되는 경우가 있는지 묻는 문제다.
hash map이나 set을 만들어 배열 수마다 그 수의 두배나 1/2이 되는 수가 있는지 확인하고 있다면 바로 true를 리턴한다.
없다면 map이나 set에 그 수를 넣어주자.
둘다 자료형을 float 나 double로 저장해주는걸 주의하자.
전체 풀이 코드
class Solution { public: bool checkIfExist(vector<int>& arr) { unordered_map<double, int> m; for (int n:arr) { if (m.find(2*n)!=m.end() || m.find((double)n/2)!=m.end()) return true; m[n] = 1; } return false; } };'LeetCode' 카테고리의 다른 글
[LeetCode] 441. Arranging Coins (0) 2021.11.05 [LeetCode] 764. Largest Plus Sign (0) 2021.11.04 [LeetCode] 404. Sum of Left Leaves (0) 2021.11.04 [LeetCode] 129. Sum Root to Leaf Numbers (0) 2021.11.03 [LeetCode] 1481. Least Number of Unique Integers after K Removals (0) 2021.11.01