Repository navigation
Expand file tree
/
Copy pathE367_ValidPerfectSquare.java
More file actions
51 lines (50 loc) · 1.63 KB
/
Copy pathE367_ValidPerfectSquare.java
File metadata and controls
51 lines (50 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package LeetCode;
public class E367_ValidPerfectSquare {
/*public boolean isPerfectSquare(int num) {
int square;
int maxSqrt=46340;
//int maxPerfectSquare=maxSqrt*maxSqrt;//int型的最大完全平方数
//System.out.println(maxPerfectSquare);
if(num>maxSqrt&&num>maxSqrt*maxSqrt)
return false;
int low=1,high=maxSqrt;
while(low<=high){
int mid=high-(high-low)/2;
//System.out.println(mid);
square=mid*mid;
//System.out.println(square);
if(square==num)
{
return true;
}else if(square>num){
//System.out.println(">");
high=mid-1;
}else {
//System.out.println("<");
low=mid+1;
}
}
return false;
}*/
/*牛顿法
https://leetcode.com/problems/valid-perfect-square/discuss/83907/3-4-short-lines-Integer-Newton-Most-Languages
*/
public boolean isPerfectSquare(int num) {
long sqrt=num;
while(sqrt*sqrt>num)
{
sqrt=(sqrt+num/sqrt)/2;
}
return sqrt*sqrt==num;
}
public static void main(String[] args){
E367_ValidPerfectSquare e367=new E367_ValidPerfectSquare();
int num=2147483647;
//int num=808201;//答案,true。平方根899
//System.out.println(Math.sqrt(num));
//System.out.println(Math.sqrt(Integer.MAX_VALUE));
//System.out.println(Integer.MAX_VALUE);
//System.out.println(Long.MAX_VALUE);
System.out.println(e367.isPerfectSquare(num));
}
}