一. 题目描写叙述

Implement int sqrt(int x).

Compute and return the square root of x.

二. 题目分析

该题要求实现求根公式,该题还算是比較简单的,由于仅仅需返回最接近的整数,直接二分法就可以。在实现的过程中还是有一些细节的,比方推断条件:x / mid > mid而不能是x > mid * mid。由于mid * mid会导致溢出。

三. 演示样例代码

  1. #include <iostream>
  2. using namespace std;
  3. class Solution
  4. {
  5. public:
  6. int sqrt(int x)
  7. {
  8. if (x == 0 || x == 1) return x;
  9. int min = 1, max = x / 2; // 根必在此区间中
  10. // 二分查找
  11. int mid, result;
  12. while (min <= max)
  13. {
  14. mid = min + (max - min) / 2;
  15. if (x / mid > mid)
  16. {
  17. // 根的平方需小于等于x,因此每次须在此处更新根的值
  18. result = mid;
  19. min = mid + 1;
  20. }
  21. else if (x / mid < mid) max = mid - 1;
  22. else return mid;
  23. }
  24. return result;
  25. }
  26. };

一些执行结果:

四. 小结

此题为分治思路的经典题型之中的一个。

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. hdu 1026 bfs+记录路径

    题意:从0,0点出发到n-1,m-1点,路上的数字代表要在这个点额外待多少秒,求最短的路 递归输出路径即可 #include<cstdio> #include<iostream> ...

  2. python开发_xml.etree.ElementTree_XML文件操作_该模块在操作XML数据是存在安全隐患_慎用

    xml.etree.ElementTree模块实现了一个简单而有效的用户解析和创建XML数据的API. 在python3.3版本中,该模块进行了一些修改: xml.etree.cElementTree ...

  3. Codeforces Round #297 (Div. 2)B. Pasha and String 前缀和

    Codeforces Round #297 (Div. 2)B. Pasha and String Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx ...

  4. MessasgePack:一个小巧高效的序列化方式

    MessagePack是一种高效二进制序列化格式.可以在多种语言中进行快速数据交换,比如JSON格式等.它比Json更加小巧,更加高效,可以用于一些结构化数据存储 ,非常适合适用于消息总线,Memor ...

  5. BZOJ 2002: [Hnoi2010]Bounce 弹飞绵羊 (动态树LCT)

    2002: [Hnoi2010]Bounce 弹飞绵羊 Time Limit: 10 Sec  Memory Limit: 259 MBSubmit: 2843  Solved: 1519[Submi ...

  6. Tasker : Scale Up/Down CPU Speed at Different Times

    http://techsplurge.com/4926/android-awesome-15-tasker-profiles-tutorials/ Yeah I know, there’s SetCP ...

  7. 【springboot+easypoi】一行代码搞定excel导入导出

    原文:https://www.jianshu.com/p/5d67fb720ece 开发中经常会遇到excel的处理,导入导出解析等等,java中比较流行的用poi,但是每次都要写大段工具类来搞定这事 ...

  8. 引用jar版本错误

    [INFO] Scanning for projects... [ERROR] The build could not read 1 project -> [Help 1] [ERROR]    ...

  9. Android事件分发机制源码分析

    Android事件分发机制源码分析 Android事件分发机制源码分析 Part1事件来源以及传递顺序 Activity分发事件源码 PhoneWindow分发事件源码 小结 Part2ViewGro ...

  10. Objective-C:继承的体现

    典型的继承例子:形状Shape为基类,继承它的类有:点类Point.圆类Circle.球体类Sphere.矩形类Rectangle.正方形类Square 点类Point也为基类,继承它的类有:圆类Ci ...