leetcode69. x 的平方根 🌟
题目:
实现 int sqrt(int x) 函数。
计算并返回 x 的平方根,其中 x 是非负整数。
由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
示例 1:
输入: 4
输出: 2
示例 2:
输入: 8
输出: 2
说明: 8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
来源:力扣(LeetCode)
解答:
- class Solution:
- def mySqrt(self, x: int) -> int:
- if x == 1: return 1
- low = 0
- high = x
- while low < high:
- cur = (high + low) // 2
- if cur * cur <= x and (cur + 1) ** 2 > x:
- return cur
- elif cur ** 2 > x:
- high = cur - 1
- else:
- low = cur + 1
- return low
leetcode69. x 的平方根 🌟的更多相关文章
- leetcode-69.x的平方根
leetcode-69.x的平方根 Points 二分查找 牛顿迭代 题意 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保 ...
- [Swift]LeetCode69. x 的平方根 | Sqrt(x)
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- LeetCode69.x的平方根
实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 输出: 2 示例 ...
- leetcode69 X的平方根的几种解法
第一种自然就是调APi啦(手动滑稽) public int mySqrt(int x) { return (int)Math.sqrt(x); } 时间是52 ms,还超过了1/5的人呢 第二种 二分 ...
- leetcode学习目录
1 leetcode-69. x 的平方根 https://www.cnblogs.com/shoshana-kong/p/9745424.html 2. 3. 4. 5. 6.
- 【leetcode-69】 x 的平方根
(主要是越界问题) 实现 int sqrt(int x) 函数. 计算并返回 x 的平方根,其中 x 是非负整数. 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去. 示例 1: 输入: 4 ...
- 牛顿法求平方根 scala
你任说1个整数x,我任猜它的平方根为y,如果不对或精度不够准确,那我令y = (y+x/y)/2.如此循环反复下去,y就会无限逼近x的平方根.scala代码牛顿智商太高了println( sqr(10 ...
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
- hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***
不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...
随机推荐
- Actor Roles 图示
Udemy上的教程<Unreal Multiplayer Mastery - Online Game Development in C++>中对Actor Roles的总结非常直观到位,一 ...
- Java继承和多态-Static关键字
1. 什么是Static 关键字? Static 能够与变量,方法和类一起使用,称为静态变量,静态方法.如果在一个类中使用static修饰变量或者方法的话,它们可以直接通过类访问,不需要创建一个类的对 ...
- 深入探究JVM(2) - 探秘Metaspace
Java 8彻底将永久代移除出了HotSpot JVM,将其原有的数据迁移至Java Heap或Metaspace.这一篇文章我们来总结一下Metaspace(元空间)的特性.如有错误,敬请指出,谢谢 ...
- leetcode-mid-Linked list-328 Odd Even Linked List-NO
mycode # Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # sel ...
- ORA-00346,借助_allow_resetlogs_corruption开库
Mon Aug 06 10:15:08 2018Errors in file /data/app/oracle/diag/rdbms/prod/prod/trace/prod_ora_6831.trc ...
- windows 10中使用命令行关掉占用指定端口的程序
通过netstat -ano与findstr命令结合查询到带有9080端口的监听信息,图中最后一列为监听程序的PID 通过tasklist命令与findstr命令结合查询到指定PID对应的应用程序 使 ...
- 使用spring提供的@Scheduled注解创建定时任务
使用方法 操作非常简单,只要按如下几个步骤配置即可 1. 导入jar包或添加依赖,其实定时任务只需要spring-context即可,当然起服务还需要spring-web: 2. 编写定时任务类和方法 ...
- WinForm 皮肤,自定义控件WinForm.UI
WinForm.UI https://github.com/YuanJianTing/WinForm.UI WinForm 皮肤,自定义控件 使用方式: BaseForm: public partia ...
- TestNG 多线程测试
TestNG以注解的方式实现多线程测试 import org.testng.annotations.Test; public class TreadDemo { // invocationCount ...
- c# 对象相等性和同一性
一:对象相等性和同一性 System.Object提供了名为Equals的虚方法,作用是在两个对象包含相同值的前提下返回true,内部实现 public class Object { public v ...