leetcode笔记:Sqrt(x)
一. 题目描写叙述
Implement int sqrt(int x).
Compute and return the square root of x.
二. 题目分析
该题要求实现求根公式,该题还算是比較简单的,由于仅仅需返回最接近的整数,直接二分法就可以。在实现的过程中还是有一些细节的,比方推断条件:x / mid > mid
而不能是x > mid * mid
。由于mid * mid
会导致溢出。
三. 演示样例代码
#include <iostream>
using namespace std;
class Solution
{
public:
int sqrt(int x)
{
if (x == 0 || x == 1) return x;
int min = 1, max = x / 2; // 根必在此区间中
// 二分查找
int mid, result;
while (min <= max)
{
mid = min + (max - min) / 2;
if (x / mid > mid)
{
// 根的平方需小于等于x,因此每次须在此处更新根的值
result = mid;
min = mid + 1;
}
else if (x / mid < mid) max = mid - 1;
else return mid;
}
return result;
}
};
一些执行结果:
四. 小结
此题为分治思路的经典题型之中的一个。
leetcode笔记:Sqrt(x)的更多相关文章
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Leetcode 笔记 36 - Sudoku Solver
题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...
- Leetcode 笔记 35 - Valid Soduko
题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...
- Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II
题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...
随机推荐
- 深入理解RESTful Web Services
RESTful的软件架构已经多火不用多说,和MVC架构一样,很多网站服务(Web Services)都遵循RESTful设计模式,那么到底什么是RESTful Web Services呢?设计一个RE ...
- 详解Object.constructor
对象的constructor属性引用了该对象的构造函数.对于 Object 对象,该指针指向原始的 Object() 函数.如下: var obj = {}; obj.constructor //ƒ ...
- Disable File System Redirector For Windows x64 (Python recipe)(转)
This disables the Windows File System Redirector.When a 32 bit program runs on a 64 bit operating sy ...
- My安装EclipseJS代码提示(Spket插件)
最近须要大量使用JS来开发,可是MyEclipse2014自带的JS编辑器没有代码提示的功能,开发效率有点低,所以安装了一个Spket的插件,过程很easy,SVN插件的安装比这个更简单. Spket ...
- arcgis新版本增加的功能
1.导出拓扑错误 2.应用图层的符号设置 3.创建 SQLite 数据库 4.最小包围体 5.图形缓冲 6.按属性分割
- .NET:CLR via C#The Managed Heap and Garbage Collection
Allocating Resources from the Managed Heap The CLR requires that all objects be allocated from the m ...
- iframe在ie和firefox中的高度兼容性问题解决
1.问题描述: <iframe src="p_photo_cont_iframe.html" name="iframe" width="700& ...
- java int类型转换成String , 不足10位 在前面补0
String.format("%010d", 25); 0代表前面要补的字符 10代表字符串长度 d表示参数为整数类型 String s = "Hello World!& ...
- java执行ping命令
public static void get() throws IOException{ String address="10.132.118.110"; Process proc ...
- Unity3d通用工具类之数据配置加载类-ini配置文件加载
Unity3d通用工具类之数据配置加载类-ini配置文件加载 上次我们讲过xml文件的加载配置管理,今天我们换个配置文件,也是比较常见的配置文件.ini格式的数据. 按照国际管理先贴一张啥是.ini文 ...