[LeetCode]69. Sqrt(x)

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

[LeetCode]69. Sqrt(x)

题目描述

这里写图片描述

思路

由于题目要求返回的是整数,简单实现的话可以使用二分
注意实现过程中的一些越界问题

代码

#include <iostream>

using namespace std;

class Solution {
public:
    int mySqrt(int x) {
        if (x == 1)
            return 1;
        int start = 0, end = x;
        while (start < end) {
            //越界点1 end + start 会越界
            int mid = ((end - start) >> 1) + start;
            //越界点2 mid * mid 会越界
            if (mid == x / mid) return mid;
            else if (start == mid || end == mid) return start;
            else if (mid < x / mid) start = mid;
            else end = mid;
        }
        return end;
    }
};

int main() {
    Solution s;
    cout << s.mySqrt(2147395599) << endl;
    system("pause");
    return 0;
}
LeetCode 69. Sqrt(x) 解题报告 LeetCode 69. Sqrt(x) 解题报告题目描述Implement int sqrt(int x).Compute and return the square root of x.示例没有给出。限制条件没有明确给出。解题思路这道题很虐心,我首先用的是牛顿迭代法解题,但是一直在2147395600和214739599这两个数出问题,于是改成用二分法,然而,还是载在这两个数据上,通过艰苦卓绝地 阅读详情

相关推荐

LeetCode力扣 69. x 的平方根 Sqrt(x) 题解代码 JavaScript

问题https://leetcode-cn.com/problems/sqrtx/ 练习使用JavaScript解答 /** * @param {number} x * @return {number} */ var mySqrt = function(x) { if(x>0 && x<4) return 1; var beg=0, end=Math.floor(x/2),mid; mid = Math.floor((b...

Coding home - 漂流瓶jz 601

[东哥的leetcode刷题日记] leetcode 69Sqrt(x)

leetcode 69Sqrt(x) 题目链接: https://leetcode-cn.com/problems/sqrtx/ 难度: 简单 归类 : 数组操作, 二分查找 题目: 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根,其中 x 是非负整数。 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 示例: 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842…, 由于返回类型是整数,小数部

qq_41847324的博客 472

Leetcode 69. Sqrt(x)

69. Sqrt(x) Total Accepted:99997Total Submissions:388728Difficulty:Medium Implementint sqrt(int x). Compute and return the square root ofx. 思路:可以直接用sqrt函数。但实际上这题考察的是二分查找,二分查...

weixin_30315435的博客 74

leetcode69 Sqrt(x),367 有效的完全平方数(python)

文章目录69. Sqrt(x)分析代码(直接使用内置函数)通过截图代码(二分查找)通过截图特殊方法(牛顿迭代法)原理代码(牛顿法)通过截图367. 有效的完全平方数分析代码(单纯开根号,再平方)失败截图代码(float.is_integer)通过截图代码(二分查找)通过截图 69. Sqrt(x) 给你一个非负整数 x ,计算并返回 x 的 算术平方根 。 由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。 注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x

命运的左轮的博客 2032

[Leetcode]69.Sqrt(x)

[leetcode] 69. Sqrt(x)题目链接:https://leetcode.com/problems/sqrtx/Implement int sqrt(int x).Compute and return the square root of x.思路二分查找,每次看中间的数平方是不是和当前数相等.class Solution { public: int mySqrt(int x)

weixin_37553155的博客 257

LeetCode69(Sqrt(x))

LeetCode69(Sqrt(x)) Implement int sqrt(int x). Compute and return the square root of x. 求 x 的平方根。我们知道 x√\sqrt{x} 是单调增函数, 1≤x√≤x1 \leq \sqrt{x} \leq x。所以可以用二分查找法来计算。int mySqrt(int x) { if

Ivan 的专栏 2964

LeetCode】#69x的平方根(Sqrt(x))

LeetCode】#69x的平方根(Sqrt(x)) 题目描述 实现 int sqrt(int x) 函数。 计算并返回 x 的平方根,其中 x 是非负整数。 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。 示例 示例 1: 输入: 4 输出: 2 示例 2: 输入: 8 输出: 2 说明: 8 的平方根是 2.82842…, 由于返回类型是整数,小数部分将被舍去。 Descrip...

weixin_43858604的博客 453

LeetCode69. Sqrt(x)

69.Sqrt(x) 题意: 实现 int sqrt(int x), 返回根号下x。 Implement int sqrt(int x). Compute and return the square root of x.我的思路: 用折半查找的方法。public int mySqrt(int x) { int first = 1; int last = x; int

YTTmiao的博客 282

LeetCode69 Sqrt(x)

LeetCode69 Sqrt(x)问题描述 Implement int sqrt(int x). Compute and return the square root of x. 简单介绍这道题的就是实现sqrt方法。可以采用二分法进行,寻找适合的整数。但是网上很多博客都再说这个超出int范围的事情,我倒认为既然平方都是int范围内的,那么这个数字肯定也在int内,而数字肯定不大于4

yanqinghe123的专栏 261

Leetcode 69. Sqrt(x) (Medium) (cpp)

Leetcode 69. Sqrt(x) (Medium) (cpp)

还是需要多学习!闷声大发财,这是坠吼的! 398

leetcode 69. Sqrt(x)

#coding=gbk ''' leetcode 69. Sqrt(x) 实现对于整数求平方根,并对求出的平方根取整数 Implement int sqrt(int x). 使用二分查找算法 ''' class Solution: def mySqrt(self, x: int) -> int: start=0 end=x//2+2 ...

WYXHAHAHA123的博客 200

69. Sqrt(x)(Leetcode刷题笔记)

69. Sqrt(x)(Leetcode刷题笔记) 文章目录69. Sqrt(x)(Leetcode刷题笔记)题目解题代码C++(核心)解题代码C++ (本地运行)算法效率 题目 给你一个非负整数 x ,计算并返回 x 的 算术平方根 。 由于返回类型是整数,结果只保留 整数部分 ,小数部分将被 舍去 。 注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 。 示例 1: 输入:x = 4 输出:2 示例 2: 输入:x = 8 输出:2 解释:8 的算术平方

lunan的博客 1166

[勇者闯LeetCode] 69. Sqrt(x)

[勇者闯LeetCode] 69. Sqrt(x)Description Implement int sqrt(int x). Compute and return the square root o x Information Tags: Binary Search | Match Difficulty: Easy Tips: 二分搜索 O(logN) Solutionclass Sol

fanguzhai的博客 491

LeetCode 69. Sqrt(x) 题解

本题是二分查找的经典应用。二分查找在有序范围中寻找解处理边界情况牛顿迭代法的应用通过本题可以深入理解二分查找的应用场景。

cannonjinx的博客 3040

LeetCode69——Sqrt(x)

LeetCode69——Sqrt(x) 题意: 实现 开根号,返回整型,如果不能完全平方,则结果返回不超过结果的最大整数。 方法:用二分法求解,分两种情况: 1.完全平方数直接返回即可。 2.不是完全平方数根据最后查找失败low与high的位置来返回结果。 注:注意0和1的情况 代码: class Solution { public: int mySqrt(int x)

上善若水 580

# [leetcode]69. Sqrt(x)

[leetcode]69. Sqrt(x) Analysis 刷题and学英语—— [ 坚持坚持] Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return t...

weixin_32135877的博客 193

LeetCode 69. Sqrt(x)(二分)

题目来源:https://leetcode.com/problems/sqrtx/ 问题描述 69.Sqrt(x) Easy Implementint sqrt(int x). Compute and return the square root ofx, wherexis guaranteed to be a non-negative integer. Since the ...

da_kao_la的博客 148

Python LeetCode 69. Sqrt(x) 二分法和牛顿迭代法

Python LeetCode 69. Sqrt(x) 二分法和牛顿迭代法 二分法 class Solution(object): def mySqrt(self, x): """ :type x: int :rtype: int """ l, r, result = 0, x, -1 while l <= r: mid = (l + r) // 2

GhostintheCode的博客 555
上一篇: [LeetCode]414. Third Maximum Number
下一篇: [LeetCode]125. Valid Palindrome
charon____
博客等级 码龄11年 3粉丝 202原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值