码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • LeetCode //C - 69. Sqrt(x)


    69. Sqrt(x)

    Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well.

    You must not use any built-in exponent function or operator.

    For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.
     

    Example 1:

    Input: x = 4
    Output: 2
    Explanation: The square root of 4 is 2, so we return 2.

    Example 2:

    Input: x = 8
    Output: 2
    Explanation: The square root of 8 is 2.82842…, and since we round it down to the nearest integer, 2 is returned.

    Constraints:
    • 0 < = x < = 2 31 − 1 0 <= x <= 2^{31} - 1 0<=x<=231−1

    From: LeetCode
    Link: 69. Sqrt(x)


    Solution:

    Ideas:
    • We first handle the special case of x being 0.
    • We define left and right to represent the range of possible square root values.
    • Inside the while loop, we calculate the mid of the range.
    • We compare mid * mid with x. To avoid integer overflow, we use mid <= x / mid.
    • If mid * mid is less than or equal to x, we update left and ans.
    • If mid * mid is greater than x, we adjust right.
    • When the loop finishes, ans contains the floor of the square root of x.
    Code:
    int mySqrt(int x) {
        if (x == 0) return 0;
        int left = 1, right = x, ans;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (mid <= x / mid) { // To avoid overflow
                left = mid + 1;
                ans = mid;
            } else {
                right = mid - 1;
            }
        }
        return ans;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    如何将等保2.0的要求融入日常安全运维实践中?
    【Linux】ubuntu系统root用户不能通过ssh连接
    超声波传感器(CHx01) 学习笔记 Ⅵ - 原始数据
    ROS program for SRI force sensor
    稳定性、可靠性、可用性、灵活性、解耦性
    51单片机LED8*8点阵显示坤坤跳舞打篮球画面
    【1990年-2022年】地级市人均GDP数据集(excel+shp)
    Jupyter Notebook又一利器nbterm,在终端玩Python!
    剑指offer(C++)-JZ19:正则表达式匹配(算法-动态规划)
    网络安全事件分级
  • 原文地址:https://blog.csdn.net/navicheung/article/details/134499096
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号