leetcode-Sqrt(x) (2014.4.7)
2014-04-13 22:06
225 查看
Implement int sqrt(int x).
Compute and return the square root of x.
注意二分搜索的写法。
class Solution {
public:
int sqrt(int x) {
long long low=0;
long long high=x/2+1;
long long middle;
while(low<=high){
middle=(high+low)/2;
long long p=middle*middle;
if(p<x){
low=middle+1;
}else if(p>x){
high=middle-1;
}else{
return middle;
}
}
return high;
}
};
Compute and return the square root of x.
注意二分搜索的写法。
class Solution {
public:
int sqrt(int x) {
long long low=0;
long long high=x/2+1;
long long middle;
while(low<=high){
middle=(high+low)/2;
long long p=middle*middle;
if(p<x){
low=middle+1;
}else if(p>x){
high=middle-1;
}else{
return middle;
}
}
return high;
}
};
相关文章推荐
- LeetCode--Sqrt(x)Python
- LeetCode:Sqrt(x)
- 【LeetCode】69. Sqrt(x)解法及注释
- LeetCode-69. Sqrt(x)
- [leetcode] Sqrt(x)
- [Leetcode]#69 Sqrt(x)
- LeetCode "Sqrt(x)"
- Leetcode | Sqrt(x)
- 【leetcode】【69】Sqrt(x)
- leetcode sqrt
- leetcode: sqrt
- 【LeetCode】69. Sqrt(x)
- 【LeetCode 69】Sqrt(x)
- LeetCode 83 Sqrt(x)
- Leetcode-69 Sqrt(x)
- [LeetCode] 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- [LeetCode]Sqrt(x)@python
- leetcode第一刷_Sqrt(x)
- 每天一道LeetCode-----重新实现开方运算sqrt(x),只返回整数部分即可