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,因为不支持unsafe
var 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))的更多相关文章
- 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 字符串转整数 ...
- leetcode刷题目录
leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- leetcode 刷题进展
最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多 前200的吃透了 足以应付非算法岗 ...
- LeetCode刷题指南(字符串)
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- LeetCode刷题总结之双指针法
Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- LeetCode刷题总结-数组篇(上)
数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...
随机推荐
- 使用 maven 创建项目模板
前言 配置 demo 工程 生成模板 生成项目 上传模板到仓库 参看链接 前言 微服务的概念越来越流行,随着服务粒度越来越细,拆分的模块越来越明确,我们的工程项目也变得越来越多. 有时候一个项目搭建, ...
- Python Ethical Hacking - BeEF Framework(2)
Basic BeEF commands: Login the BeEF Control Panel, and go to Commands page. Create Alert Dialog: Run ...
- 因为喜欢所以升级,MyStaging-3.0 继续
我为什么维护MyStaging 目前该项目只有我一个人在维护,权当学习交流.为什么要继续维护呢,说一千道一万,还是因为喜欢,由于他的简单易用,从而促使我决定对 MyStaging 进行升级,目前 3. ...
- SQL数据单条转多条(Lateral View)
Lateral View和split,explode等UDTF一起使用,它能够将一行数据拆成多行数据,并在此基础上对拆分后的数据进行聚合. 单个Lateral View语句语法定义如下:lateral ...
- Saas Erp以及分销 助手
首先贴一下相关的截图 SaasErp 登陆页 Saas Erp主页 Saas Erp 其中的商品页 Saas Erp 打印模板设计页 分销助手登录页/手势密码页/主页 1.SaaS是Software ...
- 基于Vue的UI框架element el-table表格的自定义排序
html部分: <el-table-column prop="phoneCache" label="手机缓存包编号" align="center ...
- linq介绍及工作中应用两例——左联与内联,linq循环方法
目录 1 linq介绍 1.1 linq产生背景 1.2 linq使用范围 1.3 linq核心程序集 1.4 linq架构图 1.5 linq使用形式对比 1.5.1 linq To Objects ...
- linux实现shell脚本监控磁盘内存达到阈值时清理catalina.out日志
想在服务器上写一个shell脚本,在磁盘使用率达到80%时,自动清理掉一些没有用的日志文件,根据这个想法,在生产环境上写了一个以下脚本,按照该流程,可实现在linux环境做一个定时任务来执行shell ...
- ElasticSearch(三)springboot整合ES
最基础的整合: 一.maven依赖 <parent> <groupId>org.springframework.boot</groupId> <artifac ...
- ORACLE_19c用户密码登录失败的问题以及ORA-28040
测试环境19c 本地登录无异常,创建测试用户,电脑Plsql登录提示报错ORA-28040,处理后再次登录提示密码错误,最后重置密码再次登录OK? 通过这个问题再次测试及反思: 1.ORA-28040 ...