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 ...
随机推荐
- bzoj 3473 后缀自动机多字符串的子串处理方法
后缀自动机处理多字符串字串相关问题. 首先,和后缀数组一样,用分割符连接各字符串,然后建一个后缀自动机. 我们定义一个节点代表的字符串为它原本代表的所有串去除包含分割符后的串.每个节点代表的字符串的数 ...
- flex sqlite 操作blog 二进制数据
1, 通常的操作方式: 首先我们建立表:CREATE TABLE "pages" ("id" varchar, "data& ...
- 使用socket.io+redis来实现基本的聊天室应用场景
本文根据socket.io与Redis来实现基本的聊天室应用场景,主要表现于多个浏览器之间的信息同步和实时更新. 只是简单记录了一下, 更详细的内容可以参考后续的一篇补充文章: 使用node.js + ...
- IO流-批量修改文件名称案例
/* * 源文件名: 桌面-我们今天学习IO流了哈哈哈哈-001.jpg * 修改后文件名: 桌面-000x.jpg */public class File_listFiles_upda ...
- Git_远程仓库
到目前为止,我们已经掌握了如何在Git仓库里对一个文件进行时光穿梭,你再也不用担心文件备份或者丢失的问题了. 可是有用过集中式版本控制系统SVN的童鞋会站出来说,这些功能在SVN里早就有了,没看出Gi ...
- ThinkPHP中I('post.')与create()方法的对比
简要归纳: 1.二者都可用来接收post表单提交的数据. 2.I('post.')方法可直接接收赋值给变量如$post=I('post.'),create()方法源于父类模型封装,需先实例化父类模型, ...
- 怎样在MyEclipse上耍Chrome
近期在忙着期末大作业,所以Windows App和算法的专栏都没有更了,随后这几天都会陆续開始更新的,欢迎大家的关注啦-- 在写期末大作业的时候遇到一个问题.一个新的特性在MyEclipse自带的浏览 ...
- Android 手电筒源代码
近期因为公司须要,做了一个手电筒,事实上手电筒原理非常easy,就是调用照相机的闪光灯,控制闪光灯的开关,就能够实现手电筒的效果, 强调一下,代码中一定要注意在结束的时候对闪光灯进行释放,否则就会导致 ...
- IOC详解和Unity基础使用介绍
说起IOC,可能很多初学者不知道是用来做什么的,今天正好有点时间,就来扫扫盲,顺便巩固下自己. IOC全称是Inversion Of Control,意为控制反转(这些自然百度也有),可什么是控制反转 ...
- Extjs Ajax 分页
var storeCpye = new Ext.data.GroupingStore({ proxy : new Ext.data.HttpProxy({ url : 'cxgl_cpye.app?d ...