C#LeetCode刷题之#69-x 的平方根(Sqrt(x))
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3848 访问。
实现 int sqrt(int x) 函数。
计算并返回 x 的平方根,其中 x 是非负整数。
由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
输入: 4
输出: 2
输入: 8
输出: 2
说明: 8 的平方根是 2.82842..., 由于返回类型是整数,小数部分将被舍去。
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 type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
Input: 4
Output: 2
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
示例
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3848 访问。
public class Program {public static void Main(string[] args) {var n = 8;var res = MySqrt(n);Console.WriteLine(res);n = 168;res = MySqrt2(n);Console.WriteLine(res);n = 69;res = MySqrt3(n);Console.WriteLine(res);Console.ReadKey();}private static int MySqrt(int x) {//耍赖return (int)(Math.Sqrt(x));}private static int MySqrt2(int x) {//二分逼近,最后收敛于sqrt(x)long res = x;while(res * res > x) {res = (res + x / res) / 2;}return (int)res;}private static int MySqrt3(int x) {//魔数0x5f3759df,这个解法自行百度//另外,这个解法LeetCode未AC,因为不支持unsafevar i = 0L;var num1 = 0F;var num2 = 0F;const float f = 1.5F;num1 = x * 0.5F;num2 = x;unsafe {i = *(long*)&num2;i = 0x5f3759df - (i >> 1);num2 = *(float*)&i;}num2 = num2 * (f - (num1 * num2 * num2));num2 = num2 * (f - (num1 * num2 * num2));return (int)(x * num2);}
}
以上给出3种算法实现,以下是这个案例的输出结果:
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3848 访问。
2
12
8
分析:
MySqrt 的时间复杂度依赖于运行库的实现,MySqrt2 的时间复杂度为: ,MySqrt3 的时间复杂度为: 。
C#LeetCode刷题之#69-x 的平方根(Sqrt(x))相关推荐
- LeetCode刷题实战69:x 的平方根
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- 【leetcode刷题记录】69x的平方根
class Solution {public int mySqrt(int x) {int l = 0, r = x, mid = 0, res = -1;while(l <= r) {mid ...
- LeetCode刷题实战70:爬楼梯
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- LeetCode刷题实战81:搜索旋转排序数组 II
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- leetcode 高薪_LeetCode刷题实战69:x 的平方根
算法的重要性,我就不多说了吧,想去大厂,就必须要经过基础知识和业务逻辑面试+算法面试.所以,为了提高大家的算法能力,这个公众号后续每天带大家做一道算法题,题目就从LeetCode上面选 ! 今天和大家 ...
- C#LeetCode刷题-二分查找
二分查找篇 # 题名 刷题 通过率 难度 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组的中位数(Median of Two Sorted Arrays)-该题未达最优解 30 ...
- C#LeetCode刷题-数学
数学篇 # 题名 刷题 通过率 难度 2 两数相加 29.0% 中等 7 反转整数 C#LeetCode刷题之#7-反转整数(Reverse Integer) 28.6% 简单 8 字符串转整数 (a ...
- Leetcode刷题指南和top100题目
原文链接:https://blog.csdn.net/qq_39521554/article/details/79160815 参考博文:https://blog.csdn.net/mmc2015/a ...
- LeetCode刷题笔记汇总
LeetCode刷题笔记汇总 第一次刷LeetCode写的一些笔记. 1.两数之和 3.无重复字符的最长子串 15.三数之和 18.四数之和 19.删除链表的倒数第 N 个结点 20.有效的括号 21 ...
- LeetCode 刷题之路(python版)
摘自:https://blog.csdn.net/qq_32384313/article/details/90745354 LeetCode 刷题之路(python版) 小坏wz 2019-06-02 ...
最新文章
- python列表取出元素_python中的列表,添加元素,获取元素,删除元素,列表分片,常用操作符...
- Android工程师面试该怎么准备?年薪50W
- 导入外部项目无法识别为Web项目无法部署到tomcat
- 4、mybatis主配置文件之typeAliases
- boost::mp11::mp_max_element相关用法的测试程序
- 学校计算机数据采集处理系统,一种计算机数据采集处理分析系统的制作方法
- 基于深度学习的中文语音识别系统框架(pluse)
- 再见,Kafka!RocketMQ已成气候!
- 记录——《C Primer Plus (第五版)》第十章编程练习第十二题
- Python实现Excel与XML之间的转换
- 渗透工具Sylas:数据库综合利用工具
- 草图大师SketchUp2019下载与安装教程
- scara工业机器人外观_SCARA工业机器人研究现状
- SQL 当天在当月、当季度、当年的时间进度
- (1.4.10)SXF笔试题汇总
- codeigniter配置
- 心学与技术-大学之道和尽心知性
- Linux下unzip解压文件时报错:End-of-central-directory signature not found.
- 【微信技术-微信小程序】------- 渐进式骨架屏(加载流)(第二篇)
- 北大计算机录取分数线2017,北大2017年高考在全国31省市录取分数线人数对比