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 ...
随机推荐
- 【数论】【枚举约数】【友好数】CODEVS 2632 非常好友
O(sqrt(n))枚举约数,根据定义暴力判断友好数. #include<cstdio> #include<cmath> using namespace std; int n; ...
- iOS 取消警告
第一步找到要取消的警告类型 在相应的警告上右击->Reveal in Log 被选中的-Wdeprecated-declarations就是我们所要的警告类型了. -W是前缀,这个前缀表示的是 ...
- 对象数组的初始化:null reference
今天写代码的时候,发现我写的对象数组,只声明,而没有初始化,所以记录一下这个问题:null reference. Animals [] an=new Animals[5];//这只是个对象类型数组的声 ...
- Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明
Oracle的取整和四舍五入函数——floor,round,ceil,trunc使用说明 FLOOR——对给定的数字取整数位SQL> select floor(2345.67) from dua ...
- Word绘制跨行表格
如图“用户评价电影数目”,我们需要均分第一行,选中这三个个,设置了表个高度0.5cm,但是发现上面的一个比较考上,我们需要找到水平竖直居中,那么双击表格,打开表格工具,有设计和布局,切换到布局就找到了 ...
- iOS开发学习 阶段过程简述
下面就简单介绍一下我iOS开发的感受,也是学习iOS开发的一个体系架构. 1 iOS开发环境 1.1 开发环境 标准的配置是Mac OS X + Xcode. MacOSX的话首选用苹果电脑,macm ...
- 关于Android的Build类——获取Android手机设备各种信息
经常遇到要获取Android手机设备的相关信息,来进行业务的开发,比如经常会遇到要获取CPU的类型来进行so库的动态的下载.而这些都是在Android的Build类里面.相关信息如下: private ...
- golangWEB框架gin学习之路由群组
原文地址:http://www.niu12.com/article/42 package main import ( "github.com/gin-gonic/gin" &quo ...
- sqlmapapi的跨域访问Access-Control-Allow-Origin:*;ajax
1.做sqlmapapi的二次开发时,需要通过ajax方式调用sqlmapapi,但是默认情况下,sqlmapapi是不允许跨域访问的 2.尝试增加ajax的header,修改origin的值,来避免 ...
- 理解JS中的模块规范(CommonJS,AMD,CMD)
随着互联网的飞速发展,前端开发越来越复杂.本文将从实际项目中遇到的问题出发,讲述模块化能解决哪些问题,以及如何使用 Sea.js 进行前端的模块化开发. 恼人的命名冲突 我们从一个简单的习惯出发.我做 ...