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. HDOJ 2582 f(n)

    Discription This time I need you to calculate the f(n) . (3<=n<=1000000) f(n)= Gcd(3)+Gcd(4)+… ...

  2. 【bzoj1455】【罗马游戏】左偏树+并查集(模板)

    Description 罗马皇帝很喜欢玩杀人游戏. 他的军队里面有n个人,每个人都是一个独立的团.最近举行了一次平面几何测试,每个人都得到了一个分数. 皇帝很喜欢平面几何,他对那些得分很低的人嗤之以鼻 ...

  3. 导出pem证书给服务端Push Notification使用

    1. 钥匙串创建Push证书的证书签名请求文件(CSR文件). 如下图所示: 2.创建App ID,创建Provisioning Profile,下载安装到XCode. 苹果开发者后台页面,创建Pus ...

  4. Android证书验证存漏洞 开发者身份信息可被篡改(转)

    原帖地址:http://bbs.pediy.com/showthread.php?p=1335278#post1335278 近期在国内网易,雷锋网等网站爆出谷歌市场上的索尼官方的备份与恢复应用&qu ...

  5. FSLib.Extension库

    FSLib.Extension库是一个用于.NET的扩展函数库,所提供的函数和方法均使用扩展方法引入,包含数以百计的用于日常编写程序时使用的扩展方法. http://www.fishlee.net/s ...

  6. 用Qemu模拟vexpress-a9 (五) --- u-boot引导kernel,device tree的使用

    环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...

  7. fedora25安装和docker-ce_清华源

    docker-ce_清华源 https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/fedora/ fedora25 docker-ce版本: htt ...

  8. HDU 4289 Control (最小割 拆点)

    Control Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  9. git学习——远程仓库操作

    查看当前的远程库——git remote 列出了仅仅是远程库的简单名字 可以加上-v 现实对应的克隆地址 添加远程仓库——git remote add [shortname] [url] git re ...

  10. 原生 javascript 基础回顾

    (1)打开新窗口 语法: window.open([URL], [窗口名称], [参数字符串]) 参数说明: URL:可选参数,在窗口中要显示网页的网址或路径.如果省略这个参数,或者它的值是空 字符串 ...