leetcode — sqrtx
/**
* Source : https://oj.leetcode.com/problems/sqrtx/
*
*
* Implement int sqrt(int x).
*
* Compute and return the square root of x.
*/
public class Sqrt {
/**
* 求x的平方根,这里要求的是整数
* 使用试乘法(可能存在大数乘法,会溢出)、或者试除法
* 这里使用试乘法,可以通过二分法来快速收敛
* 使用试除法可以避免大数乘法
*
* 试乘法
*
* @param x
* @return
*
*/
public int sqrt (int x) {
if (x == 0) {
return 0;
}
int mid = x / 2 + 1;
int left = 0;
int right = mid;
long temp;
while (left < right) {
temp = (long)mid * mid;
if (temp == x) {
return mid;
}
if (temp > x) {
right = mid - 1;
} else {
left = mid + 1;
}
mid = (right + left) / 2;
}
temp = right * right;
if (temp > x) {
return right - 1;
} else {
return right;
}
}
/**
* 试除法
* @param x
* @return
*/
public int sqrt1 (int x) {
if (x == 0) {
return x;
}
int i = 1;
for (; i < x; i++) {
if (i == x / i) {
return i;
} else if (i > x/ i) {
return i - 1;
}
}
return i;
}
/**
* 使用牛顿法:可计算较精确的根
* 参见:https://zh.wikipedia.org/wiki/%E7%89%9B%E9%A1%BF%E6%B3%95
*
* @param x
* @return
*/
public int sqrt2 (int x) {
if (x == 0) {
return x;
}
double current = 1;
double last = 0;
while (current != last) {
last = current;
current = (current + x / current) / 2;
}
return (int)current;
}
public static void main(String[] args) {
Sqrt sqrt = new Sqrt();
System.out.println("=========sqrt============");
System.out.println(sqrt.sqrt(9));
System.out.println(sqrt.sqrt(8));
System.out.println(sqrt.sqrt(0));
System.out.println(sqrt.sqrt(1));
System.out.println(sqrt.sqrt(Integer.MAX_VALUE));
System.out.println("=========sqrt1============");
System.out.println(sqrt.sqrt1(9));
System.out.println(sqrt.sqrt1(8));
System.out.println(sqrt.sqrt1(0));
System.out.println(sqrt.sqrt1(1));
System.out.println(sqrt.sqrt1(Integer.MAX_VALUE));
System.out.println("=========sqrt2============");
System.out.println(sqrt.sqrt2(9));
System.out.println(sqrt.sqrt2(8));
System.out.println(sqrt.sqrt2(0));
System.out.println(sqrt.sqrt2(1));
System.out.println(sqrt.sqrt2(Integer.MAX_VALUE));
}
}
leetcode — sqrtx的更多相关文章
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
- leetcode算法分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode题目分类
利用堆栈:http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/http://oj.leetcode.com/problem ...
- <转>LeetCode 题目总结/分类
原链接:http://blog.csdn.net/yangliuy/article/details/44514495 注:此分类仅供大概参考,没有精雕细琢.有不同意见欢迎评论~ 利用堆栈:http:/ ...
- [LeetCode]题解(python):069-Sqrt(x)
题目来源: https://leetcode.com/problems/sqrtx/ 题意分析: 实现一个整型的开根. 题目思路: 利用牛顿迭代法可以求解.首先讲数据类型转成浮点数,然后选取初始值为n ...
- LeetCode 题目总结/分类
LeetCode 题目总结/分类 利用堆栈: http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/ http://oj.l ...
- Leetcode 题解
Leetcode Solutions Language: javascript c mysql Last updated: 2019-01-04 https://github.com/nusr/lee ...
- leetcode math类型题目解题总结
2. Add Two Numbers https://leetcode.com/problems/add-two-numbers/description/ class Solution { publi ...
随机推荐
- Python Day 10
阅读目录: 函数 函数的使用 函数的分类 函数的返回值 ##函数 ##函数的定义-----what?----什么是函数: # 函数:完成 特定 功能的代码块,作为一个整体,对其进行特定的命名,该名字就 ...
- 制作DNS字典
1.收集字典 一般kali自带的DNS爆破工具都会有自己的字典,使用 dpkg -L dns爆破软件名 查询字典的路径.txt文件一般是字典. 合并到一个txt文件中. 2.删除字典中重复的字符串 ...
- Spring-boot在windows上安装CLI(Command Line Interface)的步骤!
首先去下载安装包,我这里整了一个zip包,一个tar包,下载地址:https://github.com/zhangyawei117/Spring-boot-CLI.git 下载完了之后,把zip包解压 ...
- js浮点数加减乘除精度不准确
做个记录,以备不时之需 //加法 Number.prototype.add = function(arg){ var r1,r2,m; try{r1=this.toString().split(&qu ...
- XML生成XAMl扩展
所有的WPF控件列为枚举 代码如: 1 public enum ControlType 2 { 3 Window_Resources, 4 Page_Resources, 5 Grid, 6 Stac ...
- 31.Stack
在Java中Stack类表示后进先出(LIFO)的对象堆栈.栈是一种非常常见的数据结构,它采用典型的先进后出的操作方式完成的.每一个栈都包含一个栈顶,每次出栈是将栈顶的数据取出,如下: Stack通过 ...
- 强大的jQGrid的傻瓜式使用方法。以及一些注意事项,备有相应的引入文件。
在介绍我的使用前,先按照国际惯例,列上网址http://blog.mn886.net/jqGrid/ 里面第一项就有相应的demo. 好,进入正题: 在学习到node.js的时候,需要使用到jQGri ...
- Java中不定项参数(可变参数)的作用和使用方式
引言: 我们在编写方法的过程中,可能会遇见一个方法有不确定参数个数的情况.一般我们会用方法重载来解决问题: //方法重载,解决参数个数不确定问题 public void method(); publi ...
- 网易彩票-我的彩票-设置-cell跳转界面
1. 点击“cell”推出对应的界面 1.1 新建group,名为:Setting 路径:MYLottery(我的彩票)->Controller 1.2 新建Cocoa Touch Class, ...
- python 二分查找法
@source_data:数据集 @binary_num:要查找的数 @mid:中间数的键值 def binary_search(source_data,search_num): #传入数据集计算中间 ...