力扣简69 x的平方根(Square root of 69 x)

纯暴力解

出现两个问题 1最开始设置i+1的平方da’yu等于x 会导致一部分测试数据出错

2最初对于测试数2147395600 一直bug 不断找问题最后才发现是后者的平方因为超int范围了

 看完题解再补充吧

package leetcode01;

import java.util.Scanner;

public class OthersSolution {

//最初对于测试数2147395600 一直bug 不断找问题最后才发现是后者的平方因为超int范围了 public static int mySqrt(int x) { int res=x; for(long i=0;i<=x;++i) { if(((i+1)*(i+1)>x)&&(i*i<=x)){ res=(int)(i); break; } } return res; }

public static void main(String[] args) { // TODO Auto-generated method stub System.out.print(mySqrt(2147395600));

}}

————————

Pure violent solution

There are two problems. 1. Setting the square da’yu of I + 1 equal to X at the beginning will lead to some test data errors

2. At first, the number of tests was 2147395600. I kept looking for problems. Finally, I found that it was the square of the latter because it was beyond the int range

Let’s add after reading the solution

package leetcode01;

import java.util.Scanner;

public class OthersSolution {

//At first, for the number of tests 2147395600, I kept looking for bugs. Finally, I found that it was the square of the latter because it was beyond the int range public static int mySqrt(int x) { int res=x; for(long i=0;i<=x;++i) { if(((i+1)*(i+1)> x)&& amp; (i*i<=x)){ res=(int)(i); break; } } return res; }

public static void main(String[] args) { // TODO Auto-generated method stub System.out.print(mySqrt(2147395600));

}}