69. Sqrt(x) (Divide-and-Conquer)
Implement int sqrt(int x).
Compute and return the square root of x.
注意: 计算平方的时候可能会溢出,所以mid要定义为long
另外,二分法初始上限不可能超过n/2+1
class Solution {
public:
int mySqrt(int x) {
int left = ;
int right = x/+;
while (left <= right)
{
long mid = left + (right - left) / ;
if (mid * mid <= x && (mid + ) * (mid + ) > x)
{
return mid;
}
else if (mid * mid < x)
left = mid + ;
else
right = mid - ;
}
return -;
}
};
69. Sqrt(x) (Divide-and-Conquer)的更多相关文章
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- [LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- [LeetCode] 124. Binary Tree Maximum Path Sum_ Hard tag: DFS recursive, Divide and conquer
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any ...
- 算法与数据结构基础 - 分治法(Divide and Conquer)
分治法基础 分治法(Divide and Conquer)顾名思义,思想核心是将问题拆分为子问题,对子问题求解.最终合并结果,分治法用伪代码表示如下: function f(input x size ...
- 算法上机题目mergesort,priority queue,Quicksort,divide and conquer
1.Implement exercise 2.3-7. 2. Implement priority queue. 3. Implement Quicksort and answer the follo ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- 【LeetCode】分治法 divide and conquer (共17题)
链接:https://leetcode.com/tag/divide-and-conquer/ [4]Median of Two Sorted Arrays [23]Merge k Sorted Li ...
- The Divide and Conquer Approach - 归并排序
The divide and conquer approach - 归并排序 归并排序所应用的理论思想叫做分治法. 分治法的思想是: 将问题分解为若干个规模较小,并且类似于原问题的子问题, 然后递归( ...
- 69. Sqrt(x) - LeetCode
Question 69. Sqrt(x) Solution 题目大意: 求一个数的平方根 思路: 二分查找 Python实现: def sqrt(x): l = 0 r = x + 1 while l ...
随机推荐
- springboot: 集成freemark模板引擎
1.freemark简介(摘自:http://blog.csdn.net/liaomin416100569/article/details/78349072) 在互联网软件内容网站中 一般首页的访问量 ...
- Matlab 之 FFT的理解和应用
网上看了一些大牛的关于FFT的见解,加上自己的一点儿理解,针对以下这几个问题来加深对FFT的理解. 不知道大家有没有类似以下几点的困惑: 问题的提出 对于1秒钟输出的连续信号,使用采样率Fs不同,就会 ...
- Rabbitmq交换器Exchange和消息队列
通常我们谈到队列服务, 会有三个概念: 发消息者.队列.收消息者,RabbitMQ 在这个基本概念之上, 多做了一层抽象, 在发消息者和 队列之间, 加入了交换器 (Exchange). 这样发消息者 ...
- servlet运作机制
最近研究zipkin,在研究客户端brave的时候,才算开始理解servlet了. servlet只是tomcat被实例化一次: 之后每次访问其实都是对同一个servlet示例操作:所以, ...
- ecmall在linux下的安装注意事项(转)
今天跟ecshop客服胡娇沟通后知道ecmall基本配置是[LAMP] linux+apache+mysql+php,然后自己开始在linux下安装ecmall并做迁移,整理了一下中间碰到的问题.1. ...
- Zend Studio 下载
http://www.52pojie.cn/thread-507229-1-1.html THINKPHP : http://www.cnblogs.com/TigerYangWTH/p/57250 ...
- python中高阶函数学习笔记
什么是高阶函数 变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作为参数,这种函数就称之为高阶函数 def fun(x, y, f): print f(x), f(y) fun ...
- CXF+Spring搭建webservice服务
Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS .这些 Services 可以支持多 ...
- java代码。。。圆的面积好搞人。。。不是一般的搞人。。。欢迎指点指点
package com.ll; public class Class3 { private String name; private int age; private int ...
- $.each $.map 和 $().each $().map
$.each :用于遍历数据,如json. $(function () { var data = [{'name': 'a', 'age': 12}, {'name': 'b', 'age': 12} ...