当前位置:网站首页>leetcode: 69. Square root of x
leetcode: 69. Square root of x
2022-08-11 07:53:00 【uncle_ll】
69. x 的平方根
来源:力扣(LeetCode)
链接: https://leetcode.cn/problems/sqrtx/
给你一个非负整数 x ,计算并返回 x 的 算术平方根 .
由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 .
注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 .
示例 1:
输入:x = 4
输出:2
示例 2:
输入:x = 8
输出:2
解释:8 的算术平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去.
提示:
- 0 <= x <= 2 31 − 1 2^{31} - 1 231−1
解法
- 库函数:This kind of general problem solving is not allowed to use library functions
- 循环:从1Start training traversal,求n*n与x的关系
- 二分法:Accelerates traversal search by dichotomy
- 牛顿法:数学公式推导
代码实现
遍历
python实现
class Solution:
def mySqrt(self, x: int) -> int:
if x == 0:
return 0
n = 1
while n * n <= x:
n += 1
return n-1
c++实现
class Solution {
public:
int mySqrt(int x) {
if ( x == 0 || x == 1)
return x;
long int n = 1;
while (n * n <= x) {
n++;
}
return n-1;
}
};
复杂度分析
- 时间复杂度: O ( N ) O(N) O(N)
- 空间复杂度: O ( 1 ) O(1) O(1)
二分法
python实现
class Solution:
def mySqrt(self, x: int) -> int:
if x == 0:
return 0
right = x
left = 0
while left <= right:
mid = left + (right-left) // 2
if mid * mid < x:
left = mid + 1
elif mid * mid == x:
return mid
elif mid * mid > x:
right = mid - 1
return min(left, right)
c++实现
class Solution {
public:
int mySqrt(int x) {
if (x == 0 || x == 1)
return x;
long int left = 0;
long int right = x;
while (left <= right) {
long int mid = left + (right-left) / 2;
if (mid * mid == x)
return mid;
else if (mid * mid < x) {
left = mid + 1;
}
else if (mid * mid > x) {
right = mid - 1;
}
}
return min(left, right);
}
};
复杂度分析
- 时间复杂度: O ( l o g N ) O(logN) O(logN)
- 空间复杂度: O ( 1 ) O(1) O(1)
牛顿法
- python实现
class Solution:
def mySqrt(self, x: int) -> int:
if x == 0:
return 0
C, x0 = float(x), float(x)
while True:
xi = 0.5 * (x0 + C / x0)
if abs(x0 - xi) < 1e-7:
break
x0 = xi
return int(x0)
- c++实现
class Solution {
public:
int mySqrt(int x) {
if (x == 0) {
return 0;
}
double C = x, x0 = x;
while (true) {
double xi = 0.5 * (x0 + C / x0);
if (fabs(x0 - xi) < 1e-7) {
break;
}
x0 = xi;
}
return int(x0);
}
};
复杂度分析
- 时间复杂度: O ( l o g N ) O(logN) O(logN) , But still faster than the dichotomy
- 空间复杂度: O ( 1 ) O(1) O(1)
参考
边栏推荐
- TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
- [Recommender System]: Overview of Collaborative Filtering and Content-Based Filtering
- 【Pytorch】nn.Linear,nn.Conv
- 基于微信小程序的租房小程序
- tf.reduce_mean() and tf.reduce_sum()
- TF通过feature与label生成(特征,标签)集合,tf.data.Dataset.from_tensor_slices
- 动态代理学习
- 1003 I want to pass (20 points)
- Write a resume like this, easy to get the interviewer
- 1.1-回归
猜你喜欢
基于微信小程序的租房小程序
Item 2 - Annual Income Judgment
【Pytorch】nn.PixelShuffle
About # SQL problem: how to set the following data by commas into multiple lines, in the form of column display
Pico neo3在Unity中的交互操作
1056 Sum of Combinations (15 points)
tf.reduce_mean() and tf.reduce_sum()
从苹果、SpaceX等高科技企业的产品发布会看企业产品战略和敏捷开发的关系
1061 判断题 (15 分)
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
随机推荐
关于Android Service服务的面试题
cdc连sqlserver异常对象可能有无法序列化的字段 有没有大佬看得懂的 帮忙解答一下
【LeetCode】Summary of linked list problems
Two state forms of Service
4.1-支持向量机
1056 Sum of Combinations (15 points)
linux 安装mysql服务报错
MindManager2022全新正式免费思维导图更新
囍楽云任务源码
tf中矩阵乘法
prometheus学习4Grafana监控mysql&blackbox了解
1036 Programming with Obama (15 points)
Write a resume like this, easy to get the interviewer
1106 2019 Sequence (15 points)
Implementation of FIR filter based on FPGA (5) - FPGA code implementation of parallel structure FIR filter
How Unity programmers can improve their abilities
go-grpc TSL认证 解决 transport: authentication handshake failed: x509 certificate relies on ... ...
接入网、承载网、核心网是什么,交换机路由器是什么、这个和网络的协议有什么关系呢?
js判断图片是否存在
【软件测试】(北京)字节跳动科技有限公司终面HR面试题