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)的更多相关文章

  1. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  2. [leetcode.com]算法题目 - Jump Game

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. [leetcode.com]算法题目 - Maximum Subarray

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  4. [leetcode.com]算法题目 - Gray Code

    The gray code is a binary numeral system where two successive values differ in only one bit. Given a ...

  5. [leetcode.com]算法题目 - Same Tree

    Given two binary trees, write a function to check if they are equal or not. Two binary trees are con ...

  6. [leetcode.com]算法题目 - Triangle

    Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent n ...

  7. [leetcode.com]算法题目 - Length of Last Word

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  8. [leetcode.com]算法题目 - Sort Colors

    Given an array with n objects colored red, white or blue, sort them so that objects of the same colo ...

  9. [leetcode.com]算法题目 - Pow(x, n)

    Implement pow(x, n). class Solution { public: double pow(double x, int n) { // Start typing your C/C ...

随机推荐

  1. jQuery之JSP加载JS文件不起作用的有效解决方法

    JSP加载JS文件不起作用的有效解决方法 作者: 字体:[增加 减小] 类型:转载 时间:2014-04-08 jsp导入jquery文件,老是不起作用,原因在于其不能访问/WEB-INF/目录下的文 ...

  2. SpringMVC学习八 @ResponseBody注解

    (一)在方法上只有@RequestMapping 时,无论方法返回值是什么认为需要跳转,代码实例如下 @RequestMapping("demo10") public People ...

  3. 2018.11.02 NOIP训练 停车场(线段树)

    传送门 这是一道困饶了我一年的题. 其实就是去年去NOIP提高组试水的时候考的模拟题 但当时我水平不够,跟ykykyk一起杠了一个下午都没调出来. 今天终于AAA了. 其实就是一个维护最长连续0101 ...

  4. 2018.10.24 NOIP模拟 小 C 的数组(二分+dp)

    传送门 考试自己yyyyyy的乱搞的没过大样例二分+dp二分+dp二分+dp过了606060把我自己都吓到了! 这么说来乱搞跟被卡常的正解比只少101010分? 那我考场不打其他暴力想正解血亏啊. 正 ...

  5. Linux关机操作

    正确的关机流程为:sync > shutdown > reboot > halt 关机指令为:shutdown ,你可以man shutdown 来看一下帮助文档. 例如你可以运行如 ...

  6. MFC控件Slider Control的使用

    写MFC界面程序时,今天恰好用到Slider控件,做一个小小的记录. 步骤 1.在工具栏中添加Slider Control控件: 2.在控件上右键->添加变量(Add Variable...), ...

  7. fork、vfork、clone

    三个都是用来创建新进程的函数 fork 概念 1)fork函数调用一次会返回两次,给父进程返回子进程的进程ID,给子进程返回0(这么设计的原因:父进程可以有很多子进程,没有一个函数可以让父进程知道所有 ...

  8. C++STL deque

    deque双端数组 deque<int> dq; deque<int>::iterator it; dq.push_back();//尾部插入元素 dq.push_front( ...

  9. excelToWord-vba

    Sub ExcelToWord() ' 利用Word程序创建文本文件,运行时word不能为打开状态 Dim WordApp As Object '搜索Dim Records As Integer, i ...

  10. 振动器(Vibrator)

    package com.wwj.serviceandboardcast;   import android.app.Activity; import android.app.Service; impo ...