Algorithm/문제
[Leetcode] Valid Perfect Sqaure
Lim Seung Hyun
2022. 11. 26. 16:24
문제 출처
Valid Perfect Square - 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
문제 해설
- 1보다 크고 2^31 - 1보다 작은 정수(num)가 어떤 수의 제곱수인지 확인하는 문제
문제 풀이
# source: https://leetcode.com/problems/valid-perfect-square/?envType=study-plan&id=binary-search-i
class Solution:
def isPerfectSquare(self, num: int) -> bool:
i = 1
while i**2 <= num:
if i**2 == num:
return True
i += 1
return False
- 1부터 하나씩 증가시켜 그 값의 제곱한 결과가 num과 같은지 확인
- 제곱한 결과가 num보다 큰 경우에는 반복을 종료
Github
728x90