Implement int sqrt(int x).

Compute and return the square root of x.

  1. class Solution {
  2. public:
  3. int sqrt(int x) {
  4. // Start typing your C/C++ solution below
  5. // DO NOT write int main() function
  6. if(x<=) return ;
  7. if(x==) return ;
  8. int small=;
  9. int large=x;
  10. int temp=x/;
  11. while(small<large){
  12. int a = x/temp;
  13. int b = x/(temp+);
  14. if (a==temp) return a;
  15. if (b==temp+) return b;
  16. if(temp<a && temp+>b)
  17. return temp;
  18. else if(temp<a && temp+<b){
  19. small=temp+;
  20. temp = (small+large)/;
  21. }else {
  22. large = temp;
  23. temp = (small+large)/;
  24. }
  25. }
  26. return -;
  27. }
  28. };

我的答案

思路:需要在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. Java SE学习【二】——面向对象

    面向对象的学习也进行了一段时间,这段时间学了,类和对象:属性:方法:封装:继承:多态:接口.也算是有一些自己的理解,不愧是贴近人类思维的思想,老师讲时我常常会想到以前的一些事物和其交相印证,其中最常想 ...

  2. jsvascript null,undefined,undeclared的区别

    1.undefined表示"缺少值",就是此处应该有一个值,但是还没有定义,转为数值时为NaN.典型用法是: (1)变量被声明了,但没有赋值时,就等于undefined. (2) ...

  3. MMS从Contacts中添加收件人显示email账号

    android系统默认代码,MMS中可以添加email地址作为收件人,但是从Contacts中选择收件人时却不显示email. 解决思路:为了降低修改量,在原来只搜索phoneNum的基础上,再做一次 ...

  4. Spring Boot学习笔记:kafka应用

    Kafka作为众多Java消息中间件之一,有诸多优点.本文讲解Kafka的应用.学习一个新的知识点,建议先找一个demo,越简单越好的demo,跑通这个demo,了解大致原理,然后在分析细节,详细了解 ...

  5. NOIP水题测试(2017082501)

    日常水题测试又来了! 以后答案都以单题形式公布. 下面看今天的水题: 时间限制:5小时 题目一:无法形容的水 题目二:比上一题还水 题目三:一元三次方程求解 题目四:单词接龙 题目五:统计单词个数 题 ...

  6. 2019.01.19 bzoj4592: [Shoi2015]脑洞治疗仪(ODT)

    传送门 ODT水题. 支持区间01赋值,区间填补(把区间[l,r][l,r][l,r]从左往右数kkk个1都变成0),区间查询最长连续1个数. 思路: 区间填补操作感觉不是很好弄,写线段树的神仙可以套 ...

  7. 2019.01.19 codeforces343D.Water Tree(树剖+ODT)

    传送门 ODTODTODT板子题. 支持子树01覆盖,路径01覆盖,询问一个点的值. 思路:当然可以用树剖+线段树,不过树剖+ODTODTODT也可以很好的水过去. 注意修改路径时每次跳重链都要修改. ...

  8. 2018.12.22 bzoj3473: 字符串(后缀自动机+启发式合并)

    传送门 调代码调的我怀疑人生. 启发式合并用迭代写怎么都跑不过(雾 换成了dfsdfsdfs版本的终于过了233. 题意简述:求给出nnn个字串,对于每个给定的字串求出其有多少个字串在至少kkk个剩下 ...

  9. offsetHeight、scrollHeight、clientHeight、height

    对这几项进行彻底研究. 第一步:纯净div,没有margin,padding,border,height设置为200px. 添加滚动条,overflow:scroll,结果div的高度被压缩,因为被滚 ...

  10. 常用模块xml,shelve,configparser,hashlib

    XML 什么XML:全称 可扩展标记语言 标记指的是代表某种含义的字符 XML<> 为什么需要XML 为能够在不同的平台间继续数据的交换 为了使交换的数据能让对方看懂 就需要按照一定的语法 ...