LeetCode-Sqrt(x)
2014-08-10 21:54
309 查看
Implement
Compute and return the square root of x.
Solution:
Code:
<span style="font-size:14px;">class Solution {
public:
int sqrt(int x) {
if (x == 0 || x == 1) return x;
unsigned long long begin = 0;
unsigned long long end = (x+1)/2;
unsigned long long mid;
unsigned long long temp;
while (begin < end) {
mid = (begin+end)/2;
temp = mid*mid;
if (temp == x) return mid;
else if (temp > x) end = mid-1;
else begin = mid+1;
}
temp = end*end;
if (temp > x)
return end-1;
else
return end;
}
};</span>
int sqrt(int x).
Compute and return the square root of x.
Solution:
Code:
<span style="font-size:14px;">class Solution {
public:
int sqrt(int x) {
if (x == 0 || x == 1) return x;
unsigned long long begin = 0;
unsigned long long end = (x+1)/2;
unsigned long long mid;
unsigned long long temp;
while (begin < end) {
mid = (begin+end)/2;
temp = mid*mid;
if (temp == x) return mid;
else if (temp > x) end = mid-1;
else begin = mid+1;
}
temp = end*end;
if (temp > x)
return end-1;
else
return end;
}
};</span>
相关文章推荐
- [Leetcode] Sqrt(x)
- [LeetCode] Sqrt(x)
- leetcode—sqrt
- leetcode——69——Sqrt(x)
- Leetcode---Sqrt(x)
- LeetCode: Sqrt(x) [069]
- 《leetCode》:Sqrt(x)----------hard
- LeetCode69 Sqrt(x)
- [leetcode]Sqrt(x)
- leetcode 69. Sqrt(x)
- LeetCode 69. Sqrt(x)(x的算术平方根)
- [Leetcode] 69. Sqrt(x) 解题报告
- LeetCode:Sqrt(x)
- [LeetCode] Sqrt(x)
- Leetcode -- Sqrt(x)
- leetcode 69 implement the sqrt(x)
- [LeetCode]Sqrt(x)
- Sqrt(x)--LeetCode
- 【leetcode】Sqrt(x)
- [LeetCode]Sqrt(x)