> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/binary-search-and-tree/69.-sqrt-x.md).

# 69. Sqrt(x)

\# Easy

> 1. 暴力解法：从1到x循环
> 2. Binary search:  用loop写替代recursive

{% tabs %}
{% tab title="Python" %}

```cpp
class Solution:
    def mySqrt(self, x: int) -> int:
        # edge case
        if x == 1:
            return 1
        
        #regular case
        start = 1
        end = x
        while start+1 < end:
            mid = start + (end-start)//2
            result = mid*mid
            if x == result:
                return mid
            if result < x:
                start = mid
            else:
                end = mid
                
        if start*start <= x:
            return start
        else:
            return end
```

{% endtab %}

{% tab title="Second Tab" %}

{% endtab %}
{% endtabs %}
