问题描述:
给定一个 正整数 num ,编写一个函数,如果 num 是一个完全平方数,则返回 true ,否则返回 false 。
进阶:不要 使用任何内置的库函数,如 sqrt 。
示例:
示例 1:
输入:num = 16
输出:true
示例 2:
输入:num = 14
输出:false
提示:
1 <= num <= 2^31 - 1
上代码,拿去即可运行:
package com.onlyqi.test03.erfen;
/**
* @author onlyqi
* @date 2022/7/11 19:42
* @description
*/
public class Test03 {
public static void main(String[] args) {
System.out.println("======================"+guess(36));
}
public static Boolean guess(int n){
int start=1;
int end =n;
while(end>=start) {
int mid = (start + end) / 2;
if(mid*mid==n){
return true;
}
if(n<mid*mid){
end=end-1;
}else {
start=start+1;
}
}
return false;
}
}
运行结果:输入36
我要刷300道算法题,第104道