(主要是越界问题)

实现 int sqrt(int x) 函数。

计算并返回 x 的平方根,其中 是非负整数。

由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。

示例 1:

输入: 4
输出: 2

示例 2:

输入: 8
输出: 2
说明: 8 的平方根是 2.82842...,
  由于返回类型是整数,小数部分将被舍去。
    public int mySqrt(int x) {
if (x==1) {
return 1;
}
for (int i=0;i<=x/2;i++) {
if (i*i == x) {
return i;
} else if (i*i<x && (i+1)*(i+1)>x) {
return i;
}
}
return 1;
}
输入
2147483647
输出
1
预期结果
46340

错误原因是数组越界,int最大开根号也就46340.9,从0~46340之间找到n,n的平方小于x,n+1的平方大于x,就好

改成下面这样

    public int mySqrt(int x) {
if (x==1) {
return 1;
}
for (int i=0;i<=x/2;i++) {
if ((long)i*(long)i == x) {
return i;
} else if ((long)i*(long)i<x && (long)(i+1)*(long)(i+1)>x) {
return i;
}
}
return 1;
}
成功
执行用时 : 51 ms, 在Sqrt(x)的Java提交中击败了20.67% 的用户
内存消耗 : 32.8 MB, 在Sqrt(x)的Java提交中击败了31.49% 的用户

【leetcode-69】 x 的平方根的更多相关文章

  1. Java实现 LeetCode 69 x的平方根

    69. x 的平方根 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: ...

  2. [leetcode] 69. x 的平方根(纯int溢出判断实现)

    69. x 的平方根 非常简单的一个题,用二分法逼近求出ans即可,额外注意下溢出问题. 不过我要给自己增加难度,用long或者BigNum实现没意思,只能使用int类型 换句话当出现溢出时我们自己得 ...

  3. LeetCode 69 x 的平方根

    链接:https://leetcode-cn.com/problems/sqrtx 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数, ...

  4. [LeetCode]69. x 的平方根(数学,二分)

    题目 https://leetcode-cn.com/problems/sqrtx 题解 方法一:牛顿迭代法 按点斜式求出直线方程(即过点Xn,f(Xn)),然后求出直线与x轴交点,即为Xn+1: 求 ...

  5. 字节笔试题 leetcode 69. x 的平方根

    更多精彩文章请关注公众号:TanLiuYi00 题目 解题思路 题目要求非负整数 x 的平方根,相当于求函数 y = √x 中 y 的值. 函数 y = √x  图像如下: 从上图中,可以看出函数是单 ...

  6. Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解

    Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...

  7. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  8. [LeetCode] 69. Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...

  9. LeetCode 69. Sqrt(x) (平方根)

    Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...

  10. 力扣(LeetCode)69. x 的平方根

    实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...

随机推荐

  1. ramdom 中的 seed 的使用

    实例 1 import ramdom # random.seed(10) # 未加 seed 的时候 for i in range(5): print(random.random()) # 每次输出结 ...

  2. HNOI2019

    省选总结 day0 写了下平时不经常写的模板,像什么\(LCT\),圆方树,\(exlucas\)之类的,但是一个都没考. day1 提前十几分钟进了考场,可以提前动电脑,赶紧把\(vimrc\)打了 ...

  3. 09 Zabbix4.0系统clone、mass update使用

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 09 Zabbix4.0系统clone.mass update使用 1. clone使用: clo ...

  4. linux统计使用最多的10个命令

    # cat /root/.bash_history  | awk '{print $1}' | sort | uniq -c | sort -nr | head history      查看命令历史 ...

  5. Speech语音播报

    System.Speech 这个命名空间,报可以阅读文字和播放音频. 环境  W10 VS2017 CMMT 1.添加程序集引用 System.Speech 2.实例化播音类,并且播放一个文本 Spe ...

  6. python中无法被转化为set的list[list组成的list]

    arr = [[a],[b]] set(arr) output: Traceback (most recent call last): File "<stdin>", ...

  7. [NOI2010]超级钢琴(RMQ+堆)

    小Z是一个小有名气的钢琴家,最近C博士送给了小Z一架超级钢琴,小Z希望能够用这架钢琴创作出世界上最美妙的音乐. 这架超级钢琴可以弹奏出n个音符,编号为1至n.第i个音符的美妙度为Ai,其中Ai可正可负 ...

  8. hdu 2609 How many(最小表示法)

    Problem Description Give you n ( n < 10000) necklaces ,the length of necklace will not large than ...

  9. [luogu3810][bzoj3262][陌上花开]

    题目链接 思路 听说可以CDQ分治,然后我不会,所以我写树套树 首先肯定先按照a拍个序.然后就成了在b,c这两个数组中查询了.用一个树状数组套treap来维护.当插入一个数的时候,就在树状数组的b这个 ...

  10. django 通过ajax完成登录

    一. 在users/views.py中加代码: from django.http import HttpResponse class LoginView(View): ""&quo ...