141. Sqrt(x) 【easy】

Implement int sqrt(int x).

Compute and return the square root of x.

Example

sqrt(3) = 1

sqrt(4) = 2

sqrt(5) = 2

sqrt(10) = 3

Challenge 

O(log(x))

解法一:

  1. class Solution {
  2. public:
  3. /*
  4. * @param x: An integer
  5. * @return: The sqrt of x
  6. */
  7. int sqrt(int x) {
  8. // write your code here
  9. long start = ;
  10. long end = x;
  11.  
  12. while (start + < end) {
  13. long mid = start + (end - start) / ;
  14.  
  15. if (mid * mid == x) {
  16. return mid;
  17. }
  18. else if (mid * mid < x) {
  19. start = mid;
  20. }
  21. else if (mid * mid > x) {
  22. end = mid;
  23. }
  24. }
  25.  
  26. if (end * end <= x) {
  27. return end;
  28. }
  29. else {
  30. return start;
  31. }
  32. }
  33. };

注意:

1、start、end、mid用long类型防止溢出

2、最后判断end * end与x的关系时要考虑等号,否则输入数值为0的时候就错了。

解法二:

  1. class Solution {
  2. public:
  3. /**
  4. * @param x: An integer
  5. * @return: The sqrt of x
  6. */
  7. int sqrt(int x) {
  8. // write your code here
  9. long left = ;
  10. if (x == )
  11. return ;
  12. long right = x;
  13. long mid = left + (right - left) / ;
  14. while (left + < right) {
  15. if (x > mid * mid) {
  16. left = mid;
  17. } else if (x < mid * mid) {
  18. right = mid;
  19. } else {
  20. return mid;
  21. }
  22. mid = left + (right - left) / ;
  23. }
  24. return left;
  25. }
  26. };

这里还是两头都判断一下比较保险。

141. Sqrt(x) 【easy】的更多相关文章

  1. 141. Linked List Cycle【easy】

    141. Linked List Cycle[easy] Given a linked list, determine if it has a cycle in it. Follow up:Can y ...

  2. 170. Two Sum III - Data structure design【easy】

    170. Two Sum III - Data structure design[easy] Design and implement a TwoSum class. It should suppor ...

  3. 160. Intersection of Two Linked Lists【easy】

    160. Intersection of Two Linked Lists[easy] Write a program to find the node at which the intersecti ...

  4. 206. Reverse Linked List【easy】

    206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...

  5. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  6. 83. Remove Duplicates from Sorted List【easy】

    83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...

  7. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  8. 142. Linked List Cycle II【easy】

    142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...

  9. 237. Delete Node in a Linked List【easy】

    237. Delete Node in a Linked List[easy] Write a function to delete a node (except the tail) in a sin ...

随机推荐

  1. 【数论】【枚举约数】【友好数】CODEVS 2632 非常好友

    O(sqrt(n))枚举约数,根据定义暴力判断友好数. #include<cstdio> #include<cmath> using namespace std; int n; ...

  2. iOS 取消警告

    第一步找到要取消的警告类型 在相应的警告上右击->Reveal in Log 被选中的-Wdeprecated-declarations就是我们所要的警告类型了. -W是前缀,这个前缀表示的是 ...

  3. 对象数组的初始化:null reference

    今天写代码的时候,发现我写的对象数组,只声明,而没有初始化,所以记录一下这个问题:null reference. Animals [] an=new Animals[5];//这只是个对象类型数组的声 ...

  4. Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明

    Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 FLOOR——对给定的数字取整数位SQL> select floor(2345.67) from dua ...

  5. Word绘制跨行表格

    如图“用户评价电影数目”,我们需要均分第一行,选中这三个个,设置了表个高度0.5cm,但是发现上面的一个比较考上,我们需要找到水平竖直居中,那么双击表格,打开表格工具,有设计和布局,切换到布局就找到了 ...

  6. iOS开发学习 阶段过程简述

    下面就简单介绍一下我iOS开发的感受,也是学习iOS开发的一个体系架构. 1 iOS开发环境 1.1 开发环境 标准的配置是Mac OS X + Xcode. MacOSX的话首选用苹果电脑,macm ...

  7. 关于Android的Build类——获取Android手机设备各种信息

    经常遇到要获取Android手机设备的相关信息,来进行业务的开发,比如经常会遇到要获取CPU的类型来进行so库的动态的下载.而这些都是在Android的Build类里面.相关信息如下: private ...

  8. golangWEB框架gin学习之路由群组

    原文地址:http://www.niu12.com/article/42 package main import ( "github.com/gin-gonic/gin" &quo ...

  9. sqlmapapi的跨域访问Access-Control-Allow-Origin:*;ajax

    1.做sqlmapapi的二次开发时,需要通过ajax方式调用sqlmapapi,但是默认情况下,sqlmapapi是不允许跨域访问的 2.尝试增加ajax的header,修改origin的值,来避免 ...

  10. 理解JS中的模块规范(CommonJS,AMD,CMD)

    随着互联网的飞速发展,前端开发越来越复杂.本文将从实际项目中遇到的问题出发,讲述模块化能解决哪些问题,以及如何使用 Sea.js 进行前端的模块化开发. 恼人的命名冲突 我们从一个简单的习惯出发.我做 ...