一. 题目描写叙述

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)的更多相关文章

  1. 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 ...

  2. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  3. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  4. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  5. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  6. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  7. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

  8. Leetcode 笔记 36 - Sudoku Solver

    题目链接:Sudoku Solver | LeetCode OJ Write a program to solve a Sudoku puzzle by filling the empty cells ...

  9. Leetcode 笔记 35 - Valid Soduko

    题目链接:Valid Sudoku | LeetCode OJ Determine if a Sudoku is valid, according to: Sudoku Puzzles - The R ...

  10. 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 ...

随机推荐

  1. 鸟哥的私房菜:Bash shell(五)-数据流重导向

    数据流重定向 数据流重导向就是将某个指令执行后应该要出现在屏幕上的数据, 给他传输到其它的地方,例如档案或者是装置 (例如打印机之类的!)!这玩意儿在 Linux 的文字模式底下可重要的! 尤其是如果 ...

  2. 【POJ】2796:Feel Good【单调栈】

    Feel Good Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 18449   Accepted: 5125 Case T ...

  3. 在Hexo中渲染MathJax数学公式

    最近学机器学习涉及很多的数学公式,公式如果用截图显示,会比较low而且不方便.因此需要对Hexo做些配置,支持公式渲染.同时文末整理了各种公式的书写心得,比如矩阵.大小括号.手动编号.上下角标和多行对 ...

  4. Codeforces Round #280 (Div. 2) E. Vanya and Field 思维题

    E. Vanya and Field time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. 【Deep Learning】一、AutoEncoder

    Deep Learning 第一战: 完成:UFLDL教程 稀疏自编码器-Exercise:Sparse Autoencoder Code: 学习到的稀疏参数W1: 参考资料: UFLDL教程 稀疏自 ...

  6. 3D数学读书笔记——3D中的方位与角位移

    本系列文章由birdlove1987编写,转载请注明出处. 文章链接: http://blog.csdn.net/zhurui_idea/article/details/25339595 方位和角位移 ...

  7. Maven实战(四)——基于Maven的持续集成实践

    Martin的<持续集成> 相信非常多读者和我一样.最早接触到持续集成的概念是来自Martin的著名文章<持续集成>.该文最早公布于2000年9月,之后在2006年进行了一次修 ...

  8. 纯文本抽出程序库DMC TEXT FILTER

    因需而生,红樱枫为文本转换市场领航 --纯文本抽出程序库DMC TEXT FILTER,从需求中把握平衡 在高度数字化的今天,数字图书馆已经成为非常多人查询资料的有效途径.然而即使在畅通的宽带搜寻中一 ...

  9. Java7语法新特性

    Java7语法新特性: 1. switch中增加对String类型的支持. public String generate(String name, String gender) { String ti ...

  10. weblogic 12C 安全加强:更改Console及管理端口

    一.管理端口的好处:1.由于我们使用的是weblogic直接提供web访问,所以防火墙无法对80端口做安全策略. 2.所有用户都可以使用http://www.reyo.cn/console/来登陆我们 ...