LeetCode—367. Valid Perfect Square

来源:互联网 发布:2017淘宝店名可以改吗 编辑:程序博客网 时间:2024/06/01 17:15

Valid Perfect Square思路:牛顿二分法,注意边界判定。

GitHub地址:https://github.com/corpsepiges/leetcode

点此进入


public class Solution {    public boolean isPerfectSquare(int num) {        int x=num;        while(true){            int t=(x+num/x)/2;            if (t*t==num||(t-1)*(t-1)==num||(t+1)*(t+1)==num) {                return true;            }            if (x==t||x==t-1||x==t+1) {                return false;            }            x=t;        }    }}


0 0
原创粉丝点击