首页 > 编程知识 正文

c语言中sqrt取整69 Sqrtx 求根号再取整

时间:2023-05-06 11:07:41 阅读:182271 作者:198

抄题:

输入sqrt (intx )。

computeandreturnthesquarerootofx,wherexisguaranteedtobeanon-negative integer。

Since the return typeis an integer,thedecimaldigitsaretruncatedandonlytheintegerpartoftheresultisreturned。

Example 1:

输入: 4

输出: 2

Example 2:

输入: 8

输出: 2

explanation : thesquarerootof8is 2.82842 .and since

the decimal part is truncated,2 is returned。

暴力解决方案:

时间分析:

空间分析:

优化后:

时间分析:

空间分析:

[奇怪的输出条件]

[奇怪的corner case] :

[思考问题]

不知道和二分法有什么关系:

找到满足第一个/最后某个条件的位置/值是二分法第二边界

[一句话的想法] :

模板

[输入量] :空:正常情况:特大:特小:程序处理的特殊情况:异常情况(非法、不合理的输入) :

绘图:

[一印] :

如果有小数,则定义为长整型

[二印] :

[三印] :

[四印] :

[五印] :

[5分钟肉眼调试的结果]

[总结] :

如果有小数,则定义为长整型

复杂度: ]:timecomplexity:o(lgn ) spacecomplexity3360o) ) ) ) ) ) ) )复杂度) ) ) 652 )

[英语数据结构和算法为什么不使用其他数据结构和算法]

找到满足第一个/最后某个条件的位置/值是二分法第二边界

重要的模板化代码

其他解决方案:

[Follow Up] :

[LC提出的主题发生了变化]

代码样式:

class solution { publicintmysqrt (intx ) {//bs

long start=1,end=x; wile (开始1 )

开始=mid;

}else{

end=mid;

() ) ) ) )。

}//return end or start

if(end*end=x ) return(int ) end;

}return(int ) start;

() ) ) ) )。

() ) ) ) )。

view代码

原文: https://www.cn blogs.com/im Miao 0319/p/8964816.html

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。