当前位置:网站首页>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;
}
}
边栏推荐
- Redis6 data type and operation summary
- Routing in laravel framework
- [server data recovery] a case of RAID5 data recovery stored in a brand of server
- Introduction to Chapter 8 proof problem of njupt "Xin'an numeral base"
- leetcode 10. Regular Expression Matching 正则表达式匹配 (困难)
- Self built shooting range 2022
- TortoiseSVN使用情形、安装与使用
- ETCD数据库源码分析——集群间网络层客户端peerRt
- Zhubo Huangyu: these spot gold investment skills are not really bad
- Resttemplate details
猜你喜欢
In addition to the root directory, other routes of laravel + xampp are 404 solutions
Ordering system based on wechat applet
Idea remote debugging agent
RK3566添加LED
Don't know these four caching modes, dare you say you understand caching?
Intranet penetration tool NetApp
Primary code audit [no dolls (modification)] assessment
Internal JSON-RPC error. {"code":-32000, "message": "execution reverted"} solve the error
French scholars: the explicability of counter attack under optimal transmission theory
ELFK部署
随机推荐
"Baidu Cup" CTF competition in September, web:upload
【MySQL 使用秘籍】一網打盡 MySQL 時間和日期類型與相關操作函數(三)
4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
js 从一个数组对象中取key 和value组成一个新的对象
2022建筑焊工(建筑特殊工种)特种作业证考试题库及在线模拟考试
web3.eth. Filter related
Ordering system based on wechat applet
Aspx simple user login
[machine learning notes] how to solve over fitting and under fitting
MySQL if else use case use
Can graduate students not learn English? As long as the score of postgraduate entrance examination English or CET-6 is high!
[notes of in-depth study paper]transbtsv2: wider instead of deep transformer for medical image segmentation
kafaka 日志收集
[js] basic syntax - for loop
那些考研后才知道的事
基于微信小程序的订餐系统
[cloud resources] what software is good for cloud resource security management? Why?
Solve the problem of "unable to open source file" xx.h "in the custom header file on vs from the source
网络安全-HSRP协议
ETCD数据库源码分析——rawnode简单封装