自己设计函数,实现求根号。x是非负整数。

Input: 8

Output: 2

当开出根号后有小数,则省略小数部分。。

思路:只要找到一个数a,a*a<=x而且(a+1)*(a+1)>x,则a就是开根号后的结果。

这里要注意:a*a因为<=x,不会溢出,但是(a+1)*(a+1)则可能会导致溢出。一种解法就是将两者结果比较,后者小于前者,说明后者溢出了(则后者应该肯定大于x),这是返回a就行。还有一种就是改写上面不等式,将乘号除到右边,变成除,就不会出现溢出。见代码

解法1:暴力解法。

  public int mySqrt(int x) {
if(x==0) return 0;
for(int i=1;i<=x;i++){
//防止溢出
if(i<=x/i&&(i+1)>x/(i+1)) return i;
}
return 0;
}

解法二:利用二分查找。

class Solution {
public int mySqrt(int x) {
if(x==0) return 0;
//二分查找
int left=1,right=x;
while(left<right){
int mid=(left+right)/2;
      //在二分查找中加入一个判断
if(mid<=x/mid&&(mid+1)>x/(mid+1))
return mid;
else if(mid>x/mid)
right=mid-1;
else
left=mid+1;
}
return left;
}
}

Implement int sqrt(int x).的更多相关文章

  1. Sqrt(int x) leetcode java

    Reference: http://blog.csdn.net/lbyxiafei/article/details/9375735  题目: Implement int sqrt(int x). Co ...

  2. int main( int argc, char **argv)

    1.参数 (有时参数是void) argc是程序运行时参数个数 argv是存储参数的数组,可以用char* argv[],也可以用char **argv. 例如编译一个hello.c的程序 1 #in ...

  3. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...

  4. int(*f)(int)

    int(*f)(int): 为指向函数的指针变量的定义方法,其中f为指向函数的指针变量,第一个int为函数返回值类型,第二个int为函数的形参类型.

  5. what is difference in (int)a,(int&)a,&a,int(&a) ?

    This interview question come from a famous communication firm of china. : ) #include <iostream> ...

  6. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别 <转>

    作者:Statmoon 出处:http://leolis.cnblogs.com/   在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为整型(int)来讲,有四种方法 ...

  7. the difference between const int *, int * const, int const *

    Some people may be confused about the sequence of const and * on declaration in C++/C, me too. Now I ...

  8. (int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    C#中(int).int.Parse().int.TryParse()和Convert.ToInt32()的区别   原文链接:http://www.cnblogs.com/leolis/p/3968 ...

  9. int main(int argc,char* argv[])详解

    argc是命令行总的参数个数 argv[]是argc个参数,其中第0个参数是程序的全名,以后的参数命令行后面跟的用户输入的参数, 比如:       int   main(int   argc,   ...

随机推荐

  1. ROS(indigo)ABB机器人MoveIt例子

    ROS(indigo)ABB机器人例子 参考网址: 1  http://wiki.ros.org/Industrial 2  http://wiki.ros.org/abb 3  https://gi ...

  2. C语言省略extern的缺陷

    在一个文件中(比如a.c)定义一个全局变量int a = 10; 然后在另一个代码文件(比如main.c)中需要使用变量a,可以写 int a; 单独看main.c文件时就会出现二义性,一个含义是当其 ...

  3. scala学习笔记3(trait)

    // trait 类似于 Java8 中可以带 default method 的接口. // trait 中可以带有实现的方法,也可以带有抽象的方法,使用 trait 的方式是 with 而混入类中 ...

  4. J2EE进阶(三)struts2 <s:action>标签的用法

    J2EE进阶(三)struts2 <s:action>标签的用法 前言 使用action标签,可以允许在jsp页面中直接调用Action,(类似AJAX页面调用)在调用Action时候,可 ...

  5. Uva - 230 - Borrowers

    AC代码: #include <iostream> #include <cstdio> #include <cstdlib> #include <cctype ...

  6. CUDA程序的调试总结【不定时更新】

    1 )CUDA的程序,经常犯,但是很难发现的一个错误就是同步问题. 描述下实例 for (k = 0; k < N; k+=BS) { sda[tx] = gda[tx+index]; __sy ...

  7. 连接器与容器的桥梁——CoyoteAdapter

    如果把整个tomcat内核最高抽象程度模块化,可以看成是由连接器Connector和容器Container组成,连接器负责HTTP请求接收及响应,生成请求对象及响应对象并交由容器处理,而容器则根据请求 ...

  8. ROS_Kinetic_11 ROS程序基础Eclipse_C++(二)

    ROS_Kinetic_11 ROS程序基础Eclipse_C++(二) 编写简单的Service和Client (C++): http://wiki.ros.org/cn/ROS/Tutorials ...

  9. 【一天一道LeetCode】#33. Search in Rotated Sorted Array

    一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...

  10. 【一天一道LeetCode】#63. Unique Paths II

    一天一道LeetCode (一)题目 Follow up for "Unique Paths": Now consider if some obstacles are added ...