Leetcode_sqrtx
2014-03-30 15:42
387 查看
地址:http://oj.leetcode.com/problems/sqrtx/
Implement
Compute and return the square root of x.
思路:主要是处理2147483647这个case吧。同时也要注意上溢出的问题,不然也会TLE。
两个参考代码,一个152ms,一个96ms
Implement
int sqrt(int x).
Compute and return the square root of x.
思路:主要是处理2147483647这个case吧。同时也要注意上溢出的问题,不然也会TLE。
两个参考代码,一个152ms,一个96ms
//152ms class Solution { public: int sqrt(int x) { if(x<=1) return x; int i = 2; for(; i*i<=x &&i*i>0; ++i) { if((i+100)*(i+100)>0 &&(i+100)*(i+100)<x) { i+=100; } } return i-1; } }; //96ms class Solution { public: int sqrt(int x) { if(x<=1) return x; int i = 2; for(; i*i<=x &&i*i>0; ++i) { if((i+1000)*(i+1000)>0 &&(i+1000)*(i+1000)<x) { i+=1000; } } return i-1; } };
相关文章推荐
- 【Leetcode】之sqrt(x)
- Leetcode-sqrt
- 【LeetCode】69. Sqrt(x)
- leetcode--Sqrt(x)
- [LeetCode]Sqrt(x)
- LeetCode:Sqrt(x) 解题报告
- leetcode69 Sqrt(x)
- leetcode 69 sqrt(x)
- leetcode 69. Sqrt(x)
- Sqrt(x)——LeetCode
- [LeetCode] Sqrt(x) 解题报告
- [LeetCode] Sqrt(x) 解题报告
- leetcode 53: Sqrt(x)
- 【LeetCode】Sqrt(x)
- LeetCode - sqrt 题解
- LeetCode-- Implement int sqrt(int x)
- leetcode-69. Sqrt(x)
- Leetcode NO.69 Sqrt(x)
- LeetCode-Sqrt(x)
- leetcode - Sqrt(x)