[Leetcode]-- Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

 

[Leetcode]-- Sqrt(x)
public class Solution {
    public int sqrt(int x) {
        int start = 0;
        int end = x;
        
        if(x == 0 ||  x == 1) return x;
        
        while(start <= end){
            int m = (start+end)/2;
            if(m == x/m){
                return m;
            }else if(m > x/m){
                end = m -1;
            }else{
                start = m+1;
            }
            
        }
        
        return (start+ end)/2;
    }
}
[Leetcode]-- Sqrt(x)

[Leetcode]-- Sqrt(x)

上一篇:wpf:DataGrid使用


下一篇:.net 发展阶段