[leetcode.com]算法题目 - Sqrt(x)
Implement int sqrt(int x).
Compute and return the square root of x.
class Solution {
public:
int sqrt(int x) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(x<=) return ;
if(x==) return ;
int small=;
int large=x;
int temp=x/;
while(small<large){
int a = x/temp;
int b = x/(temp+);
if (a==temp) return a;
if (b==temp+) return b;
if(temp<a && temp+>b)
return temp;
else if(temp<a && temp+<b){
small=temp+;
temp = (small+large)/;
}else {
large = temp;
temp = (small+large)/;
}
}
return -;
}
};
我的答案
思路:需要在O(log n)时间内实现,并且注意用除法,防止两个int型的数相乘超过int的范围。
[leetcode.com]算法题目 - Sqrt(x)的更多相关文章
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- [leetcode.com]算法题目 - Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- [leetcode.com]算法题目 - Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [leetcode.com]算法题目 - Gray Code
The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...
- [leetcode.com]算法题目 - Same Tree
Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...
- [leetcode.com]算法题目 - Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...
- [leetcode.com]算法题目 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...
- [leetcode.com]算法题目 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...
- [leetcode.com]算法题目 - Pow(x, n)
Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...
随机推荐
- cocos jsb工程转html 工程
1 CCBoot.js prepare方法:注掉下面这行,先加载moduleConfig中的脚本后加载user脚本 //newJsList = newJsList.concat(jsList); // ...
- UVA 10821 Constructing BST
BST: binary search tree. 题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemi ...
- 20155312 2016-2017-2《Java程序设计》课程总结
20155312 2016-2017-2<Java程序设计>课程总结 每周作业链接汇总 预备作业1:你期望的师生关系是什么? 预备作业2:做中学learning by doing个人感想 ...
- Blob CLOB区别
区别: CLOB :使用char来保存数据.例如xml文件.文章或者较长的文字. BLOB:就是使用二进制保存数据.例如保存位图.图片音乐. 联系:两者可以互相转换.或者直接用lob字段代替两者. 读 ...
- sqlserver 修改数据库表所有者
SQLServer修改表所有者 来自:http://www.cnblogs.com/tearer/archive/2012/09/16/2687802.html 批量修改:EXEC sp_MSfor ...
- (16)The beauty of what we'll never know
https://www.ted.com/talks/pico_iyer_the_beauty_of_what_we_ll_never_know/transcript 00:13One hot Octo ...
- (6)How language shapes the way we think
https://www.ted.com/talks/lera_boroditsky_how_language_shapes_the_way_we_think/transcript 00:12So, I ...
- Vue + Element UI 实现权限管理系统
Vue + Element UI 实现权限管理系统 前端篇(一):搭建开发环境 https://www.cnblogs.com/xifengxiaoma/p/9533018.html
- 移动赋值运算符(c++11)
1.概念 1)移动赋值运算符是一个重载的赋值运算符,参数为自身类的右值引用,返回值自身类的左值引用,由于不抛出任何异常,用noexcept指定(如果定义在类的外面,那么定义也要用noexcept指定) ...
- vue中的路由嵌套
1定义组件 const Index = { template:` <div>首页</div> ` } const Order = { template:` <div> ...