力扣算法题—069x的平方根
实现 int sqrt(int x)
函数。
计算并返回 x 的平方根,其中 x 是非负整数。
由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
示例 1:
输入: 4
输出: 2
示例 2:
输入: 8
输出: 2
说明: 8 的平方根是 2.82842...,
由于返回类型是整数,小数部分将被舍去。
#include "_000库函数.h" //最简单想法,耗时长
class Solution {
public:
int mySqrt(int x) {
if (x <= )return x;
double n = ;//防止int溢出
while (++n) {
if (n*n > x) {
--n;
return (int)n;
}
else if (n*n == x)
return (int)n;
}
return ;//力扣非得要在这里加一个return,无语了
}
}; //用对折法
//因为x为int,最大为65536的平方
//贼鸡儿快,内存很少
class Solution {
public:
int mySqrt(int x) {
if (x <= )return x;
double min = , max = , mid = (int)((min + max)/);//防止int溢出
while (min < max&&mid*mid != x) {
if (mid*mid < x)min = mid + ;
else max = mid - ;
mid = (int)((min + max)/);
}
if (mid*mid > x)--mid;
return (int)mid;
}
}; //同样折半,更简洁
class Solution {
public:
int mySqrt(int x) {
if (x <= ) return x;
int left = , right = x;
while (left < right) {
int mid = left + (right - left) / ;
if (x / mid >= mid) left = mid + ;
else right = mid;
}
return right - ;
}
}; //更牛逼的方法
//用牛顿迭代法,记得高数中好像讲到过这个方法,是用逼近法求方程根的神器,
//因为要求x2 = n的解,令f(x)=x2-n,相当于求解f(x)=0的解,可以求出递推式如下:
//
//xi + 1 = xi - (xi2 - n) / (2xi) = xi - xi / 2 + n / (2xi) = xi / 2 + n / 2xi = (xi + n / xi) / 2 class Solution {
public:
int mySqrt(int x) {
long res = x;
while (res * res > x) {
res = (res + x / res) / ;
}
return res;
}
};
void T069() {
Solution s;
cout << "2: " << s.mySqrt() << endl;
cout << "8: " << s.mySqrt() << endl;
cout << "0: " << s.mySqrt() << endl;
cout << "9: " << s.mySqrt()<<endl;
}
力扣算法题—069x的平方根的更多相关文章
- 力扣算法题—060第K个排列
给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列. 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下: "123" "132&qu ...
- 力扣算法题—050计算pow(x, n)
#include "000库函数.h" //使用折半算法 牛逼算法 class Solution { public: double myPow(double x, int n) { ...
- 力扣算法题—147Insertion_Sort_List
Sort a linked list using insertion sort. A graphical example of insertion sort. The partial sorted l ...
- 力扣算法题—093复原IP地址
给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: ["255.255.11.135", ...
- 力扣算法题—079单词搜索【DFS】
给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用. ...
- 力扣算法题—052N皇后问题2
跟前面的N皇后问题没区别,还更简单 #include "000库函数.h" //使用回溯法 class Solution { public: int totalNQueens(in ...
- 力扣算法题—051N皇后问题
#include "000库函数.h" //使用回溯法来计算 //经典解法为回溯递归,一层一层的向下扫描,需要用到一个pos数组, //其中pos[i]表示第i行皇后的位置,初始化 ...
- 力扣算法题—143ReorderList
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You may not mod ...
- 力扣算法题—144Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
随机推荐
- Spring Boot搭建Web项目常用功能
搭建WEB项目过程中,哪些点需要注意: 1.技术选型: 前端:freemarker.vue 后端:spring boot.spring mvc 2.如何包装返回统一结构结果数据? 首先要弄清楚为什么要 ...
- JQuery官方学习资料(译):类型
类型 JavaScript提供了多个内置数据类型.除了这些,这份文档还将介绍一些虚拟类型,例如选择器.伪类.事件等. String 在JavaScript中字符串是一个不可变的对象,它包含无. ...
- No.3 数组中重复的数字 (P39)
题目1:找出数组中重复的数字 [题目描述] 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个 ...
- Vue Document
目录 VUE笔记 环境搭建 Vue学习笔记 1.Vue指令 VUE笔记 环境搭建 node -v npm -v npm i -g cnpm --registry=https://registry.np ...
- JavaScript如何正确处理Unicode编码问题!
原文:JavaScript 如何正确处理 Unicode 编码问题! 作者:前端小智 Fundebug经授权转载,版权归原作者所有. JavaScript 处理 Unicode 的方式至少可以说是令人 ...
- 2017 ACM-ICPC西安网赛B-Coin
B-Coin Bob has a not even coin, every time he tosses the coin, the probability that the coin's front ...
- crontab -e 怎么保存后退出?
退出 ctrl + z
- babel在项目里的使用
1.手动在项目里创建文件 .babelrc 2.安装 $ npm install --save-dev babel-cli # ES2015转码规则 $ npm install --save-dev ...
- JavaScript Array常用属性和方法
Array的length属性可以通过赋值改变,但这样会导致Array原有的大小发生改变. var a = ["I", "Love", "You&quo ...
- 通过JS生成由字母与数字组合的随机字符串
在项目中可能需要随机生成字母数字组成的字符,如生成3-32位长度的字母数字组合的随机字符串(位数不固定)或者生成43位随机字符串(位数固定) 使用Math.random()与toString()方法的 ...