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. Problem D: 零起点学算法24——判断奇偶数

    #include<stdio.h> int main() { int a; while(scanf("%d",&a)!=EOF) ==) printf(&quo ...

  2. Problem B: 零起点学算法17——2个数比较大小

    #include<stdio.h> int main() { int n,m; while(scanf("%d %d",&n,&m)!=EOF) if( ...

  3. Problem T: 零起点学算法15——交换变量

    #include<stdio.h> int main() { int a,b,c; scanf("%d %d",&a,&b); c=a; a=b; b= ...

  4. Scala实战高手****第17课:Scala并发编程实战及Spark源码阅读

    package com.wanji.scala.test import javax.swing.text.AbstractDocument.Content import scala.actors.Ac ...

  5. hadoop InputSplit

    /** * <code>InputSplit</code> represents the data to be processed by an * individual {@l ...

  6. Spring.NET的IoC容器(The IoC container)——简介(Introduction)

    简介 这个章节介绍了Spring Framework的控制反转(Inversion of Control ,IoC)的实现原理. Spring.Core 程序集是Spring.NET的 IoC 容器实 ...

  7. 【mybatis】count 计数查询 + List的IN查询

    mybatis中conut计数的sql怎么在mapper中写? Mapper.java类这么写 @Mapper public interface GoodsBindConfigMappingMappe ...

  8. golangWEB框架gin学习之获取post参数

    原文地址:http://www.niu12.com/article/41 package main import ( "fmt" "github.com/gin-goni ...

  9. http://blog.sina.com.cn/s/blog_62e1faba010147k4.html

    http://blog.sina.com.cn/s/blog_62e1faba010147k4.html

  10. [Unity-2] Unity播放音乐

    Unity里面大部分的功能都能够通过拖拽来实现,可是为了方便介绍,在这里都通过代码来实现.  Unity里面要播放音乐主要有下面3个要素: 1.AudioSource:控制音乐播放的主体 2.Audi ...