首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
求平方根
[编程题]求平方根
热度指数:160537
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
实现函数
int mysqrt(int x).
计算并返回 x 的平方根(向下取整)
数据范围:
要求:空间复杂度
,时间复杂度
示例1
输入
2
输出
1
示例2
输入
2143195649
输出
46294
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(18)
邀请回答
收藏(584)
分享
提交结果有问题?
190个回答
135篇题解
开通博客
漫漫云天自翱翔
发表于 2021-07-18 20:18:16
精华题解
题解一:二分 题解思路: 二分查找比a<=mysqrt(x)<=b 如果 mid*mid <=x 且(mid+1)(mid+1) <x 返回mid 如果mid*mid > x right = mid-1; 否则 lef
展开全文
Peterliang
发表于 2021-07-18 23:36:43
精华题解
题意分析 这个题目题目意思很简洁,就是让我们求出一个数的开根号之后的数据,向下进行取整。 思路分析 解法一 二分 我们发现,最后的答案具有单调性,当我们求出某一个数字为mid的时候,我们可以和给出的数字进行比较,判断这个数字是大了还是小了,从而移动我们的边界。这样通过左右边界的移动我们最后就可
展开全文
下一次什么时候可以修改昵称
发表于 2020-10-18 20:08:41
算法 二分查找 1.初始范围为1,x 2.当middle*middle <= x && (middle+1)*(middle+1) > x时,返回结果 3.当middle*middle < x时,到右半部分继续寻找 4.当middle*middle > x时,
展开全文
天生撅墙
发表于 2020-09-04 11:19:51
Java 求数 n 的平方根 如果一个数 i,i * i 小于 n,(i + 1) * (i + 1)大于 n,那么这个数就是 n 的平方根。 从 1 开始遍历即可。代码如下:``` public int mysqrt (int x) { // write code here if (x <
展开全文
有理想的咕咕
发表于 2020-12-20 19:16:57
思路:熟悉工具类的都知道,Math里面有个floor就是向下取整的而且参数为double类型,而int转double是没问题的,然后向下取整后,double类型强转回int也不会有精度缺失了。。。 public int mysqrt (int x) { // write code h
展开全文
华科不平凡
发表于 2020-08-10 14:04:47
二分法,注意考虑溢出 class Solution { public: /** * * @param x int整型 * @return int整型 */ int mysqrt(int x) { // write code he
展开全文
LifelongCode
发表于 2021-02-04 17:54:23
解法1:根据平方数的性质——连续n个奇数相加的结果一定是平方数。 如:9=1+3+5;16=1+3+5+7;所以,不断的进行奇数相加,并判断x大小即可 public class Solution { public int mysqrt(int x) { int i=1;
展开全文
ajaj
发表于 2021-07-13 13:16:00
思路: 从题中给出的有效信息: 取x的平方根向下取整 最好不要使用库函数,这是面试官不想看到的,仔细分析此题,不难发现此题有两个信息点:1.x>4时,x的平方根一定不会超过它的一半,2.自然数的平方是递增的,它是一个有序的排序;基于两点信息可以使用 二分法 来进行求解此题 方法一:二分法
展开全文
Edwin_Xu
发表于 2020-08-26 00:19:33
public int mysqrt(int m) { double x = m; for (int i = 0; i < 20; i++) { x = x - ((xx -m)/(2x)); } return (in
展开全文
君无颜
发表于 2022-03-17 11:09:16
二分法,然后挨着比,不要用 mid*mid,会溢出,用 x/mid 即可 python实现 class Solution: def mysqrt(self , x: int) -> int: # write code here if x == 1:
展开全文
牛客493206717号
发表于 2021-03-07 15:51:42
题目描述实现函数 int mysqrt(int x).计算并返回x的平方根(向下取整) 解法 //解法:二分查找 //时间:O(logx) 空间O(1) int mysqrt(int x) { if (x <= 0) return 0; int
展开全文
牛客82035003号
发表于 2022-02-17 20:28:04
int Sqrt(int x ) { if(x < 2) return&nb
展开全文
问题信息
二分
基础数学
难度:
190条回答
584收藏
27975浏览
热门推荐
通过挑战的用户
查看代码
110020
2023-02-18 16:04:45
牛客62354...
2022-11-24 17:50:02
马孔多在下雨2022
2022-10-02 19:12:00
在做ppt的伊...
2022-09-19 10:32:38
苦行僧人
2022-09-18 22:55:29
相关试题
远亲不如近邻
排序
二分
评论
(13)
牛牛的超市
动态规划
基础数学
评论
(5)
分组
基础数学
二分
评论
(11)
如图 1 表示使用快表(页表)的虚...
编程基础
评论
(1)
订单表order_table全部记...
查找
数据库
数据分析
SQL
评论
(2)
求平方根
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ int mysqrt(int x) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param x int整型 # @return int整型 # class Solution: def mysqrt(self , x ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ function mysqrt( x ) { // write code here } module.exports = { mysqrt : mysqrt };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param x int整型 # @return int整型 # class Solution: def mysqrt(self , x: int) -> int: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ func mysqrt( x int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ int mysqrt(int x ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param x int整型 # @return int整型 # class Solution def mysqrt(x) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ def mysqrt(x: Int): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ fun mysqrt(x: Int): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ public int mysqrt (int x) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ export function mysqrt(x: number): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ func mysqrt ( _ x: Int) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param x int整型 * @return int整型 */ pub fn mysqrt(&self, x: i32) -> i32 { // write code here } }
2
1
2143195649
46294