首页
随机
最近更改
特殊页面
社群首页
参数设置
关于WHY42
免责声明
WHY42
搜索
用户菜单
登录
欢迎来到Riguz的小站!这是一个私人wiki,用来记录一些我的笔记。
查看“︁69.Sqrt(x)”︁的源代码
←
69.Sqrt(x)
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
=Description= {{LeetCode |id=sqrtx |no=69 |difficulty=Easy |category=Math |collection=Top 150 |title=Sqrt(x) |summary=Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.}} You must not use any built-in exponent function or operator. For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python. Example 1: <syntaxhighlight lang="bash"> Input: x = 4 Output: 2 </syntaxhighlight> Explanation: The square root of 4 is 2, so we return 2. Example 2: <syntaxhighlight lang="bash"> Input: x = 8 Output: 2 </syntaxhighlight> Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned. ===Binary search=== {{Submission|runtime=1ms|memory=40.82MB|rp=86.98|mp=40.25}} <syntaxhighlight lang="bash"> class Solution { public int mySqrt(int x) { int l = 1, r = x; while(l <= r){ int m = (r-l)/2 + l; if(m > x/m) r = m-1; else if(m < x/m) l = m+1; else return m; } return r; } } </syntaxhighlight> [[Category:Algorithm]] [[Category:Math]] [[Category:LeetCode]]
此页面嵌入的页面:
模板:LeetCode
(
查看源代码
)
模板:Submission
(
查看源代码
)
返回
69.Sqrt(x)
。