当前位置:网站首页>LeetCode_ 69 (square root of x)
LeetCode_ 69 (square root of x)
2022-07-05 14:13:00 【***】
Title Description :
Give you a nonnegative integer x , Calculate and return x Of Arithmetical square root .
Because the return type is an integer , Results are retained only Integral part , The decimal part will be Give up .
Be careful : No built-in exponential functions and operators are allowed , for example pow(x, 0.5) perhaps x ** 0.5 .
Example 1:
Input :x = 4
Output :2
Example 2:
Input :x = 8
Output :2 explain :8 The arithmetic square root of is 2.82842…, Because the return type is an integer , The decimal part will be removed .
Tips :
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;
}
}
边栏推荐
猜你喜欢
治臻新能源冲刺科创板:年营收2.2亿 上汽创投是股东
物联网应用技术专业是属于什么类
Current situation, trend and view of neural network Internet of things in the future
Oneconnect listed in Hong Kong: with a market value of HK $6.3 billion, ye Wangchun said that he was honest and trustworthy, and long-term success
牛客网:拦截导弹
Recommendation number | what are interesting people looking at?
Sqllab 1-6 exercise
Sorter evolution of ticdc 6.0 principle
Xampp configuring multiple items
Zhizhen new energy rushes to the scientific innovation board: the annual revenue is 220million, and SAIC venture capital is the shareholder
随机推荐
神经网络物联网未来现状和趋势及看法
The speed monitoring chip based on Bernoulli principle can be used for natural gas pipeline leakage detection
How to call the function mode of one hand and one machine
Tidb DM alarm DM_ sync_ process_ exists_ with_ Error troubleshooting
R语言使用ggplot2包的geom_histogram函数可视化直方图(histogram plot)
一网打尽异步神器CompletableFuture
3W principle [easy to understand]
R Language ggplot2 Visualization: visualize linegraph, using Legend in Theme function. Paramètre de position emplacement de la légende personnalisée
Why do mechanical engineers I know complain about low wages?
LeetCode_3(无重复字符的最长子串)
-Web direction attack and defense world
After the microservice project is deployed, static resources and files uploaded to upload cannot be accessed. Solution
[buuctf.reverse] 152-154
Anchor navigation demo
UE source code reading [1]--- starting with problems delayed rendering in UE
金融壹账通香港上市:市值63亿港元 叶望春称守正笃实,久久为功
TiFlash 面向编译器的自动向量化加速
OSI and tcp/ip protocol cluster
Deep copy is hard
Use the word "new" to attract curious people