码迷,mamicode.com
首页 > 其他好文 > 详细

367. Valid Perfect Square

时间:2018-04-09 21:10:55      阅读:114      评论:0      收藏:0      [点我收藏+]

标签:problem   star   false   static   square   class   div   problems   http   

原题链接: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

标签:problem   star   false   static   square   class   div   problems   http   

原文地址:https://www.cnblogs.com/optor/p/8761800.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!