当前位置:网站首页>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
边栏推荐
- Redis realizes single sign on -- system framework construction (I)
- Kubernetes -- Introduction to common plug-ins of kubernetes
- 【上位机教程】CANopen通信下一体化步进电机与台达PLC(AS228T)的应用
- New function | intelligent open search online customized word weight model
- 学习pinia 介绍-State-Getters-Actions-Plugins
- B+树索引使用(7)匹配列前缀,匹配值范围(十九)
- LeetCode 217. 存在重复元素
- vector的一些实用操作
- 父组件访问子组件的方法或参数 (子组件暴漏出方法defineExpose)
- Example of establishing socket communication with Siemens PLC based on C # open TCP communication
猜你喜欢

Visual stdio(VS)中的(int argc、char** argv)命令行参数

The difference between $route and $route

同站攻击(相关域攻击)论文阅读 Can I Take Your Subdomain?Exploring Same-Site Attacks in the Modern Web

一笔画问题(中国邮递员问题)

火山引擎云上增长方案全景:30+方案齐出,兵发优势领域

【花雕动手做】有趣好玩的音乐可视化系列小项目(13)---有机棒立柱灯

A college archives management system based on asp.net
2022 employment season! Adobe helps creative industry workers break through the shackles of skills and return to the source of ability

1-6月中国ADAS供应商占比9% 又一家零部件巨头全面布局智驾新赛道

Learn about Pinia state getters actions plugins
随机推荐
基于ASP.NET的某高校学院档案管理系统
Who is responsible for the problems of virtual idol endorsement products? And listen to the lawyer's analysis
12 brand management of commodity system in gulimall background management
一笔画问题(中国邮递员问题)
Can MySQL customize variable parameter storage functions?
How to optimize a large number of if else [email protected] Detailed explanation of valib
Today's sleep quality record 75 points
Panorama of volcanic engine cloud growth plan: 30 + plans come out together, and military development advantage areas
被罚“带薪休假”一个月后,谷歌解雇了“爱”上 AI 的他
【花雕动手做】有趣好玩的音乐可视化系列小项目(12)---米管快速节奏灯
Redis realizes single sign on -- system framework construction (I)
RMII, smii, gmii, rgmii interfaces of Ethernet Driver
MySQL可以自定义变参存储函数吗?
Guys, please ask me, I have configured CDC to connect to Oracle according to the document, and I always run error reports and can't find the class validstione
Detailed explanation of redisson distributed lock process (II)
Elementary notes of binary tree
Niuke brush sql---2
B+树(5)myISAM简介 --mysql从入门到精通(十七)
Bitwise and how to write SQL
JSON格式执行计划(6)—mysql执行计划(五十二)