当前位置:网站首页>Square root of leetcode 69. x
Square root of leetcode 69. x
2022-07-26 13:11:00 【PUdd】
LeetCode 69. x The square root of
Their thinking
Dichotomy (left, middle, right), With left > right Is a termination condition
Consider special circumstances :left == right Also enter when while loop , here middle*middle == x( Or because of decimals middle*middle <= x), First use ans Save the results , The next entry into the cycle does not meet the conditions .
C++ Code
class Solution {
public:
int mySqrt(int x)
{
int left = 0, right = x, ans=-1;
while (left <= right)
{
int middle = (left+ right)/2;
if ((long long)middle*middle <= x)
{
ans = middle;// in order to middle*middle==x, Directly jump out of the loop
left = middle + 1;
}
else
{
right = middle - 1;
}
}
return ans;
}
};
The two methods originally used : Violence law , And not used left > right The dichotomy of this judgment condition
边栏推荐
- 目标检测网络R-CNN 系列
- Kubernetes----安装部署NFS服务器
- Flutter prevents scientific counting and removes mantissa invalid 0
- 【TypeScript】TypeScript常用类型(上篇)
- The difference between $route and $route
- Kubernetes---- installing and deploying NFS servers
- StreamNative 团队文化:一家“透明”的公司
- Chat system based on webrtc and websocket
- 基于C#实现的学生考试系统
- Emotion analysis model based on Bert
猜你喜欢
![[5g] what are Cu and Du in 5g?](/img/5b/3453ade88ded4593edfcba9beb8ada.jpg)
[5g] what are Cu and Du in 5g?

0 basic programming resources (collect first ~ read slowly ~)

Student examination system based on C #

基于ASP.NET的某高校学院档案管理系统

被罚“带薪休假”一个月后,谷歌解雇了“爱”上 AI 的他

Shutter background graying effect, how transparency, gray mask

解决方案丨5G技术助力搭建智慧园区
![[5gc] what is 5g slice? How does 5g slice work?](/img/8c/52ba57d6a18133e97fa00b6a7cf8bc.png)
[5gc] what is 5g slice? How does 5g slice work?

牛客刷SQL---2

基于C#实现的学生考试系统
随机推荐
Use grid to realize left, middle and right layout, and the middle content is adaptive
Student examination system based on C #
Kubelet CRI container runtime
B+树(5)myISAM简介 --mysql从入门到精通(十七)
Food safety | these common foods are poisonous! Check your dining table quickly
How to remove underline and color when there is focus in the shutter textfield
Food safety | is self-made food purchased online healthy food? Don't fall into these misunderstandings
Kubernetes Flannel:HOST-GW模式
目标检测网络R-CNN 系列
Router. Push(), router. Reply(), router. Go()
【5G】5G中的CU和DU是什么?
Kubernetes----高级存储之PV和PVC简介
Is the account opened by flush safe?
Kubernetes----PV和PVC的生命周期简介
子组件触发父组件自定义事件(defineEmits):子组件传值给父组件
华为年内二度招聘“天才少年”;540万Twitter账号信息泄露,卖价3万美元;谷歌解雇了相信AI有意识的工程师|极客头条...
Flutter prevents scientific counting and removes mantissa invalid 0
Kubernetes----安装部署NFS服务器
MySQL可以自定义变参存储函数吗?
Solution: unable to load the file c:\users\user\appdata\roaming\npm\npx PS1, because running scripts is prohibited on this system.