当前位置:网站首页>LeetCode_69(x 的平方根 )
LeetCode_69(x 的平方根 )
2022-07-05 13:51:00 【***】
题目描述:
给你一个非负整数 x ,计算并返回 x 的 算术平方根 。
由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。
注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 。
示例 1:
输入:x = 4
输出:2
示例 2:
输入:x = 8
输出:2 解释:8 的算术平方根是 2.82842…, 由于返回类型是整数,小数部分将被舍去。
提示:
0 <= x <= 231 - 1
class Solution {
public int mySqrt(int x) {
int ans=-1;
if(x==0)return 0;
for (int i = 1; i <=x; i++) {
if(x/i==i){
ans=i;
break;
}
else if((x/i)>i&&(x/(i+1))<(i+1)){
ans=i;
break;
}
}
return ans;
}
}
边栏推荐
猜你喜欢
uplad_ Labs first three levels
stm32逆向入门
Jetpack Compose入门到精通
Embedded software architecture design - message interaction
研究生可以不用学英语?只要考研英语或六级分数高!
The "Baidu Cup" CTF competition was held in February 2017, Web: explosion-2
PHP basic syntax
Jasypt configuration file encryption | quick start | actual combat
Set up a website with a sense of ceremony, and post it to the public 2/2 through the intranet
运筹说 第68期|2022年最新影响因子正式发布 快看管科领域期刊的变化
随机推荐
stm32逆向入门
Xampp configuring multiple items
Those things I didn't know until I took the postgraduate entrance examination
Laravel framework operation error: no application encryption key has been specified
ELFK部署
[public class preview]: basis and practice of video quality evaluation
Wonderful express | Tencent cloud database June issue
Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
kafaka 日志收集
Programmer growth Chapter 8: do a good job of testing
::ffff:192.168.31.101 是一个什么地址?
Selenium crawls Baidu pictures
Integer = = the comparison will unpack automatically. This variable cannot be assigned empty
Controller in laravel framework
不知道这4种缓存模式,敢说懂缓存吗?
In addition to the root directory, other routes of laravel + xampp are 404 solutions
Summit review | baowanda - an integrated data security protection system driven by compliance and security
PHP basic syntax
Parsing XML using Dom4j
搭建一个仪式感点满的网站,并内网穿透发布到公网 2/2