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 ...
随机推荐
- 02.02.01 第1章 简介及基础操作(Power BI商业智能分析)
02.02.01.01 powerbi简介 00:10:59 02.02.01.02 query数据导入 00:03:26 具体操作实例如下: 02.02.01.03导入access数据 00:05: ...
- istio实现对外暴露服务
1.确认istio-ingressgateway是否有对外的IP kubectl get service istio-ingressgateway -n istio-system 如果 EXTERNA ...
- 第43章:MongoDB-集群--Sharding(分片)--多机的搭建
①环境准备 服务器规划 服务器[192.168.0.75] 服务器[192.168.0.84] 服务器[192.168.0.86] mongos mongos mongos config server ...
- C++: cin
cin字符的时候, 会忽略掉'\n', ' '等空白符
- 多线程.Thread.Sleep方法
多线程执行中,调用Thread.Sleep()方法 分情况: 1. 单核的情况下 是把当前正在工作的主线程停止(也就是从把线程变成非工作线程). 其他需要工作的线程来争夺CPU这个闲下来的核.谁争夺到 ...
- docker install
1.安装必要工具集 sudo yum install -y yum-utils 2.安装Docker官方源 sudo yum-config-manager \ --add-repo \ https:/ ...
- 安装Kali的小问题
安装时候的问题 1.安装时程序步骤不正确 在安装kali时,参照视频教程(https://www.mosoteach.cn/web/index.php?c=res&m=index&cl ...
- 28.实现 strStr() 函数
28.实现 strStr() 函数 给定一个 haystack 字符串和一个 needle 字符串,在 haystack 字符串中找出 needle 字符串出现的第一个位置 (从0开始).如果不存在, ...
- 谈一款MOBA类游戏《码神联盟》的服务端架构设计与实现(更新优化思路)
注:本文仅用于在博客园学习分享,还在随着项目不断更新和完善中,多有不足,暂谢绝各平台或个人的转载和推广,感谢支持. 一.前言 <码神联盟>是一款为技术人做的开源情怀游戏,每一种编程语言都是 ...
- Javascript高级编程学习笔记(30)—— BOM(4)navigator对象
window对象作为浏览器的全局对象.location对象保存了页面的url信息 那么navigator对象又有什么作用呢? navigator对象 该对象最早由 Netspace Navigator ...