[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 ...
随机推荐
- .net从网络接口地址获取json,然后解析成对象(一)
整理代码,今天遇到一个问题,就是从一个场景接口获取json,然后解析成对象.之前的时候都好好的,这次返回的json字符串里,由于字符编码的问题,格式上不能转换.一直以为是解析的过程编码有误,试了utf ...
- 链栈的基本操作(C语言)
栈的链式储存结构称为链栈.链栈的节点类型与链式线性表的节点类型 定义相同,不同的是它是仅在表头进行操作的单链表.链栈通常用不带头节 点的单链表来实现,栈顶指针就是链表的头指针 ,如图所示: 代码如下: ...
- Mysql导入excel数据,解决某些特殊字符乱码问题
问题 做项目需要从excel表格导入到mysql的数据库表中,excel表格中的“规格”字段的“×”符号导入数据库表中,会出现部分数据的“×”这个符号会乱码,成“?”的形式. 解决方法 打开excel ...
- 2019.01.10 bzoj1095: [ZJOI2007]Hide 捉迷藏(动态点分治)
传送门 蒟蒻真正意义上做的第一道动态点分治! 题意:给一棵最开始所有点都是黑点的树,支持把点的颜色变成从黑/白色变成白/黑色,问当前状态树上两个最远黑点的距离. 思路: 首先考虑不带修改一次点分治怎么 ...
- 负载均衡下 tomcat session 共享
概述 在分布式部署的情况下,每台tomcat 都会有自己的session ,这样如果 用户A 在tomcat1 下登录,在tomcat2 下并没有session信息.如果 tomcat1宕机,tomc ...
- OEM-ODM-OBM
Original Equipment ManufacturerOriginal Design ManufacturerOwn Branding & Manufacturing OEM,即“原始 ...
- arguments[0]()的详解
var length = 10; function fn(){ console.log(this.length); } var obj = { length:5, method:function(fn ...
- innodb mvcc多版本实现
出自:http://hedengcheng.com/?p=148 基本知识 假设对于多版本(MVCC)的基础知识,有所了解.InnoDB为了实现多版本的一致读,采用的是基于回滚段的协议. 行结构 In ...
- Ubuntu Server 命令行下的默认语言改为英语en_US.UTF-8
源文链接:http://tonychiu.blog.51cto.com/656605/393131 如果Ubuntu Server在安装过程中,选择的是中文(很多新手都会在安装时选择中文,便于上手), ...
- WordPaster2产品介绍
更新WordParser组件,集成ImagePaster组件功能.完善粘贴剪帖板图片,粘贴电脑图片文件,粘贴word,粘贴excel逻辑. 大幅度优化和精简js代码,js代码行数从1932行减少到97 ...