LeetCode---(69)Sqrt(x)
来源:互联网 发布:四维数据的图形表示 编辑:程序博客网 时间:2024/05/22 01:46
Implement int sqrt(int x)
.
Compute and return the square root of x.
利用二分法查找:第一种可能性是直接找到能够麻烦要求的数;第二种可能性是找到相邻的两个数,可以比较两个数哪一个离target更近,不过题目当中希望找的是更小的那个数。
class Solution {public: int mySqrt(int x) { if(x<=1) return x; int left=1; int right=x; unsigned long long temp=0; while(left<=right) { int mid=(left+right)/2; if(mid==x/mid) return mid; else if(mid<x/mid) left=mid+1; else right=mid-1; } 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)
- Sqrt(x) - LeetCode 69
- 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)
- 黑马程序员——基础学习(十三)IO流
- L3:equals()、String 类
- Project Euler:Problem 69 Totient maximum
- C语言程序设计7--8章
- Jar ------META-INF的介绍
- LeetCode---(69)Sqrt(x)
- C++慎用define,以及相关的替代的方法
- 无限互联 奖学金 连载1北京总部 49期学员肖建祥
- TOJ 3858 Home 搜索
- MFC资源切换(AFX_MANAGE_STATE)简介
- C语言中的数组和字符串
- 循环有序数组的查找
- 归档—SharedPreferences(轻量级存储)
- (面试加分题)OC中block的基本用法与深入了解