코딩 공부/Leetcode
[Python] 1346. Check If N and Its Double Exist
일하는 공학도
2025. 1. 26. 12:20
728x90
난이도 : easy
arr에 있는 요소 중 2배인 원소가 있으면 true를 return
class Solution:
def checkIfExist(self, arr: List[int]) -> bool:
ans = []
for i in arr:
if i * 2 in ans or i / 2 in ans:
return True
ans.append(i)
return False
ans라는 리스트를 설정하고, ans 리스트 내에 2배나 1/2인 원소가 있으면 true를 return.
중복되지 않도록 일부러 if문 뒤에 append를 추가
Runtime : 3ms (42.22%)
Memory : 18.05MB (6.75%)
728x90