Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
[367] Valid Perfect Square
문제 설명
Given a positive integer num, return true if num is a perfect square or false otherwise.
A perfect square is an integer that is the square of an integer. In other words, it is the product of some integer with itself.
You must not use any built-in library function, such as sqrt.
Example
코드 설명
if (num < 2) { return true; }0과 1은 완전 제곱수이므로 true를 반환합니다.
long left = 2, right = num / 2, mid, square;이진 검색을 위한 초기 범위를 설정합니다. left는 2, right는 num의 절반으로 초기화합니다.
while (left <= right) { ... }이진 검색을 수행합니다.
mid = left + (right - left) / 2;현재 범위의 중간 값을 계산합니다.
square = mid * mid;중간 값의 제곱을 계산합니다.
if (square == num) { return true; }제곱이 num과 같으면 true를 반환합니다.
else if (square > num) { right = mid - 1; }제곱이 num보다 크면 범위를 왼쪽으로 좁힙니다.