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))

解法一:

 class Solution {
public:
/*
* @param x: An integer
* @return: The sqrt of x
*/
int sqrt(int x) {
// write your code here
long start = ;
long end = x; while (start + < end) {
long mid = start + (end - start) / ; if (mid * mid == x) {
return mid;
}
else if (mid * mid < x) {
start = mid;
}
else if (mid * mid > x) {
end = mid;
}
} if (end * end <= x) {
return end;
}
else {
return start;
}
}
};

注意:

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

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

解法二:

 class Solution {
public:
/**
* @param x: An integer
* @return: The sqrt of x
*/
int sqrt(int x) {
// write your code here
long left = ;
if (x == )
return ;
long right = x;
long mid = left + (right - left) / ;
while (left + < right) {
if (x > mid * mid) {
left = mid;
} else if (x < mid * mid) {
right = mid;
} else {
return mid;
}
mid = left + (right - left) / ;
}
return left;
}
};

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

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. UVA 1514 Piece it together (二分图匹配)

    [题目链接] Link [题目大意] 给你一些由一块黑块和两块白块组成的L形拼图,问你是否能够拼成给出的图 [题解] 我们将所有的黑块拆点,拆分为纵向和横向,和周围的白块连边, 如果能够得到完美匹配, ...

  2. 【线段树】Gym - 100507C - Zhenya moves from parents

    线段树每个结点维护两个值,分别是这个区间的 负债 和 余钱. 按时间顺序从前往后看的时候,显然负债是单调不减的. 按时间顺序从后往前看的时候,显然余钱也是单调不减的,因为之前如果有余钱,可能会增加现在 ...

  3. 【最大流】【Dinic】bzoj1711 [Usaco2007 Open]Dingin吃饭

    把牛拆点,互相连1的边. 把牛的食物向牛连边,把牛向牛的饮料连边. 把源点向牛的食物连边,把牛的饮料向汇点连边. 要把牛放在中间,否则会造成一头牛吃了自己的食物后又去喝别的牛的饮料的情况. #incl ...

  4. 继续推荐几款VisualStudio的插件(二)

    今天晚上闲着的时候逛了一下,发现了几款不错的VisualStudio插件,这里推荐一下: Exception Breaker 在调试的时候,为了及时发现错误,我们常常会打开"总是引发所有CL ...

  5. IE刷新后,文本框的值不变

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Swift,数组

    1.创建(Array)数组(数组内的类型一定要相同,有序的可重复) (1)创建默认值的数组 let array:[Int] array=[Int](repeatElement(3,count:5)) ...

  7. C#之Raw Socket网络封包监视源码

    大家可以建立一个Windows Form应用程序,在下面的各个文件中添加对应的源码: //RawSocket.csnamespace ReceiveAll{ using System; using S ...

  8. 一起來玩鳥 Starling Framework(5)Multi-Touch

    這篇來談談Starling的Multi-Touch.前一篇也提到,Multi-Touch一樣是監聽TouchEvent.TOUCH,然後由TouchEvent的e.getTouches()取回多點的資 ...

  9. Yii2.0源码分析之——控制器文件分析(Controller.php)创建动作、执行动作

    在Yii中,当请求一个Url的时候,首先在application中获取request信息,然后由request通过urlManager解析出route,再在Module中根据route来创建contr ...

  10. 【千纸诗书】—— PHP/MySQL二手书网站后台开发之基础知识

    前言: 在具体回顾每一个功能的实现前,还是有必要先温习一些项目涉及到的PHP.MySQL[语法基础].项目github地址:https://github.com/66Web/php_book_stor ...