141. Sqrt(x) 【easy】
141. Sqrt(x) 【easy】
Implement int sqrt(int x).
Compute and return the square root of x.
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】的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 206. Reverse Linked List【easy】
206. Reverse Linked List[easy] Reverse a singly linked list. Hint: A linked list can be reversed eit ...
- 203. Remove Linked List Elements【easy】
203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...
- 83. Remove Duplicates from Sorted List【easy】
83. Remove Duplicates from Sorted List[easy] Given a sorted linked list, delete all duplicates such ...
- 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 ...
- 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 ...
- 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 ...
随机推荐
- POJ 2315:Football Game(博弈论)
[题目链接] http://poj.org/problem?id=2315 [题目大意] 两名球员轮流从N个球中挑出不多于M个射门,每个球半径都是R,离球门S. 每次只能踢出L以内的距离.进最后一个球 ...
- [CF494D]Birthday
题意:给一棵带边权的树,定义如下的一些东西 $S(x)$表示以$x$为根的子树中的节点组成的集合 $d(u,v)$表示$u$和$v$之间的距离 $f(u,v)\sum\limits_{x\in S(v ...
- 【分块】【链表】bzoj2738 矩阵乘法
http://www.cnblogs.com/jianglangcaijin/p/3460012.html 首先将矩阵的数字排序.设置size,每次将size个数字插入.插入时,我们用h[i][j]记 ...
- Exercise01_01
public class print{ public static void main(String[] args){ System.out.println("Welcome to Java ...
- N进制加法
我是网络公司的一名普通程序员,英文名Steven,发音比较像“师弟”,自从入职培训自我介绍后,大家就称我为“二师弟”,我喜欢看科幻小说,也喜欢做梦,有一次梦到外星球,发现外星人使用的并非10进制/16 ...
- SQLSERVER WINDBG调试:mssqlwiki.com
https://mssqlwiki.com/2012/10/16/sql-server-exception_access_violation-and-sql-server-assertion/ SQL ...
- Telnet窗口尺寸选项
转:http://www.cnpaf.net/Class/Telnet/200408/6.html 1.命令名称和选项代码 名称=NAWS(NegotiateAboutWindowSize)协商窗口的 ...
- asp.mvc展示model
1. ASP.Net MVC 3 Model 简介 通过一简单的事例一步一步的介绍2. ASP.Net MVC 3 Model 的一些验证 MVC 中 Model 主要负责维持数据状态,将数据从数据存 ...
- nodeJs常用API
1.url (1)url.parse返回url对象的各种参数 url.parse(url,true/false,true/false);//默认url.parse(url,false,false); ...
- LibreOffice创建数据透视表
LibreOffice创建数据透视表 LibreOffice: 选中数据,Data->Pivot Table->Create,拖拽行.列到Row和column,Data Filed要点击O ...