标签:问题: int 并且 一个 lse java public cts code
java版:
class Solution { private int low=0; private int high=0; public boolean isPerfectSquare(int num) { if(num==1)return true; low=0; high=num>>1; while(low<=high) { long mid = (low+high)>>1; long res=mid*mid; if(res>num)high=(int) (mid-1); else if(res<num)low=(int)(mid+1); else if(res==num)return true; } return false; } }
/*** * 问题:有效的完全平方数 * 原理:一个数为完全平方数则必是某个数的平方 * 并且这个数要小于等于它的一半,如4为2的平方 */ class Solution { private: int low=0; int high=0; public: bool isPerfectSquare(int num) { low=0; high=num>>1; while(low<=high) { int mid=(low+high)>>1; int res=mid*mid; if(num>res)low=mid+1; else if(num<res)high=mid-1; else if(num==res) { return true; } } return false; } };
标签:问题: int 并且 一个 lse java public cts code
原文地址:https://www.cnblogs.com/z2529827226/p/11747741.html