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 ...
随机推荐
- linux修改用户id,组id
一.修改用户uid usermod -u foo 二.修改用户gid groupmod -g 2005 foo usermod -g 2005 foo 三.检查 cat /etc/passwd su ...
- HTML的Tomcat
修改D:\software\apache-tomcat-8.0.44\webapps\ROOT\WEB-INF\web.xml: <?xml version="1.0" en ...
- 致C#,致我这工作一年(上)
回忆 最近比较闲,虽然我总是每天会在博客园逛上1~2个钟(最近是真的有点闲),看了很多人对于工作的感悟,谈程序员的职业规划,不知不觉出来工作我也快一年多了,我也想聊聊现在用C#找工作和我这一年多 ...
- Maven 项目 启动时 解决3 字节的 UTF-8 序列的字节 3 无效
"org.activiti.bpmn.exceptions.XMLException: 3 字节的 UTF-8 序列的字节 3 无效." Maven 项目启动时,由于读XML配置文 ...
- 未能加载文件或程序集“ .....WebUI ”或它的某一个依赖项,试图加载格式不正确的程序
编译Web网站没有问题(需要引用oracle.dataAccess.dll),在运行时报错如下: 解决: 1. 将项目编译生成x86模式(win7 64位) 2. 有可能本机运行有问题,发布到IIS, ...
- asp.net 抽象方法和虚方法的用法区别,用Global类重写Application_BeginRequest等方法为例子
不废话,直接贴代码 public abstract class LogNetGlobal : System.Web.HttpApplication { protected void Applicati ...
- Final——无线网络密码破解——WPA/WPA2
Final--无线网络密码破解--WPA/WPA2 20154305 齐帅 ↓ ↓ ↓ * # % & 郑 重 声 明 & % # * ↓ ↓ ↓ 本实验教程用于探索无线路由安全漏洞, ...
- python property对象
一.从@porperty说起 Python内置的@property装饰器是负责把一个方法变成属性调用的 class Stu(object): def __init__(self,age): self. ...
- 记录一下msf的学习使用
刚刚用Metasploit Pro scan了一下云端服务器.RHOST直接输IP就好. 得到反馈如下: [*] [2019.04.04-14:27:35] Scan initiated: Speed ...
- 机器学习常用sklearn库
Sklearn.model_selection(模型选择) Cross_val_score:交叉验证 Train_test_split:数据切割 GridsearchCV:网格搜索 Sklearn.m ...