367. Valid Perfect Square

Posted optor

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了367. Valid Perfect Square相关的知识,希望对你有一定的参考价值。

原题链接:https://leetcode.com/problems/valid-perfect-square/description/
实现如下:

/**
 * Created by clearbug on 2018/2/26.
 */
public class Solution {

    public static void main(String[] args) {
        Solution s = new Solution();
        System.out.println((long) Integer.MAX_VALUE * Integer.MAX_VALUE);
        System.out.println(s.isPerfectSquare(Integer.MAX_VALUE));
        System.out.println(s.isPerfectSquare(16));
    }

    public boolean isPerfectSquare(int num) {
        if (num < 1) {
            return false;
        }
        if (num == 1) {
            return true;
        }
        int start = 1;
        int end = num;
        while (start <= end) {
            int medium = start + (end - start) / 2;
            long multi = (long) medium * medium;
            if (multi == num) {
                return true;
            } else if (multi > num) {
                end = medium - 1;
            } else {
                start = medium + 1;
            }
        }

        return false;
    }

}

以上是关于367. Valid Perfect Square的主要内容,如果未能解决你的问题,请参考以下文章

367. Valid Perfect Square

367. Valid Perfect Square

367. Valid Perfect Square

leetcode 367. Valid Perfect Square

Leetcode 367. Valid Perfect Square

[leetcode] 367. Valid Perfect Square