LeetCode 69. Sqrt(x)
来源:互联网 发布:cacti添加linux主机 编辑:程序博客网 时间:2024/06/08 17:59
二分法求开方,比较简单,如果找到更加快速的方法就更好了。有如下几种,迭代牛顿法、另外一种有cramack提出的办法。
这里只列出我的二分法,其他方法我专门找一次总结。
int fun(int c,int x) { if(x/c==c)return 0; return (x/c)>c?-1:1; } int mySqrt(int x) { long long temp = x; //if(temp>INT_MAX)x = INT_MAX; //cout<<x<<endl; double res; int left,right,mid=0; left = 1,right=x; int l,r,m; if(x==1)return 1; else if(x==0)return 0; while(left<=right){ //cout<<left<<" "<<right<<endl; mid=(left+right)/2; m = fun(mid,x); if(m==0)return mid; else if(m<0)left=mid+1; else if(m>0)right=mid-1; } //cout<<r<<l<<endl; return right; }
0 0
- LeetCode 69. Sqrt(x)
- LeetCode 69.Sqrt(x)
- LeetCode --- 69. Sqrt(x)
- [Leetcode] 69. Sqrt(x)
- [leetcode] 69.Sqrt(x)
- Leetcode-69.Sqrt(x)
- leetcode--69.sqrt(x)
- LeetCode 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- 【LeetCode】69. Sqrt(x)
- 69. Sqrt(x) LeetCode
- [leetcode] 69. Sqrt(x)
- leetcode 69. Sqrt(x)
- LeetCode 69. Sqrt(x)
- LeetCode 69. Sqrt(x)
- leetcode-69. Sqrt(x)
- [leetcode]69. Sqrt(x)
- LeetCode-69.Sqrt(x)
- 用2个栈实现队列
- Android插件化开发 第二篇 [动态加载apk优化]
- spring-boot研究三:springboot+mybatis+mysql多数据源配置
- 【NOIP2016提高A组模拟8.14】总结
- 安装findbugs
- LeetCode 69. Sqrt(x)
- 关于Android GC的一些理解
- js正则验证0~1之间数字的方法。可以为小数,0和1
- learn word2vector from scratch -- part 2
- iOS从入门开发系列01 (UI基础控件)
- LeetCode | Palindrome Partitioning I,II
- 电视直播网站小记
- 设计模式之桥梁模式--- Pattern Bridge
- SPOJ PHRASESRelevant Phrases of Annihilation