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 ...
随机推荐
- HDOJ 4085 Peach Blossom Spring
discriptionTao Yuanming(365-427) was a Chinese poet of Eastern Jin dynasty. One of his most famous w ...
- POJ 2348 Euclid's Game(博弈论)
[题目链接] http://poj.org/problem?id=2348 [题目大意] 给出两个数,两个参赛者轮流用一个数减去另一个数的倍数,当一个数为0的时候游戏获胜, 求先手是否必胜 [题解] ...
- []ARC098
咕咕咕 C:普及组难度的题 D:给定$a_{1\cdots n}$,求有多少$1\leq l\leq r\leq n$满足$x_l+\cdots+x_r=x_l\text^\cdots\text^x_ ...
- [xsy1294]sub
给出一棵$N$个节点的无根树,节点$i$有权值$v_i$.现在有$M$次操作,操作有如下两种: $1\ x\ y$ 将节点$x$的权值$v_x$修改为$y$ $2$ 选择一个联通块(也可以不选择),使 ...
- 【二分答案】【字符串哈希】bzoj2084 [Poi2010]Antisymmetry
显然只有偶数长度的串符合题意,并且如果一个串符合题意,那么从其首尾各截掉一个字符也符合题意. 于是枚举中心,二分可以向左右扩展的最远距离,累计答案. #include<cstdio> #i ...
- 8.1(java学习笔记)注解(Annotation)
一.Annotation Annotation不是程序本身,但它可以对程序进行解释,这一点和注释类似. 但最大的不同点在于,注解可以被其他程序读取,然后可以对其进行一些有针对性操作. 这是注解与注释最 ...
- 5.2类集(java学习笔记)Map,Set接口
一.Map接口 Map接口中存储数据是通过key->value的方式成对存储的,可以通过key找到value. 二.Map接口常用子类 1.HashMap HashMap是无序存放的,key不允 ...
- mongodb_profier
http://docs.mongodb.org/manual/reference/database-profiler/ 一.获取.设置profile(profile用collection存储数据) d ...
- ORACLE查看并修改最大连接数的具体步骤
第一步,在cmd命令行,输入sqlplus 第二步,根据提示输入用户名与密码 1. 查看processes和sessions参数 SQL> show parameter processes ...
- Android线程与线程池
引言 在Android中,几乎完全采用了Java中的线程机制.线程是最小的调度单位,在很多情况下为了使APP更加流程地运行,我们不可能将很多事情都放在主线程上执行,这样会造成严重卡顿(ANR),那么这 ...