367. Valid Perfect Square - Easy

前往題目

想法

  • 經典的Binary Search

思路

  1. 經典Binary Search

Code

class Solution {
    public boolean isPerfectSquare(int num) {
        int l = 1, r = num;

        while (l <= r) {
            int mid = l + (r - l) / 2;
            long tmpSquare = (long) mid * (long) mid;

            if (tmpSquare > num) {
                r = mid - 1;
            } else if (tmpSquare < num) {
                l = mid + 1;
            } else {
                return true;
            }
        }
        return false;
    }
}

367. Valid Perfect Square - Easy
https://f88083.github.io/2024/09/27/367-Valid-Perfect-Square-Easy/
作者
Simon Lai
發布於
2024年9月27日
許可協議