---
title: "[leetcode] sqrt(x)"
author: "standalone"
date: "2013-04-15T07:44:01Z"
tags: []
categories: ["编程语言"]
slug: "leetcode-sqrt-x"
draft: false
lastmod: "2013-04-15T07:44:01Z"
id: 1847368
url: "https://standalone.iteye.com/blog/1847368"
---

I have talked about this question in my previous posts.

http://leetcode.com/onlinejudge#question_69
Q:
Implement int sqrt(int x).

Compute and return the square root of x.

Solution:


```java

public class Solution {
    public int sqrt(int x) {
        // Start typing your Java solution below
        // DO NOT write main() function
        double error = 0.0000001f;
        double high = x;
        double low = 0;
        while(high-low> error){
            double mid = (high+low)/2;
            if(mid*mid>x){
                high = mid;
            }else {
                low = mid;
            }
        }
        return (int)Math.floor(high);
    }
}

```