(二分查找 拓展) leetcode 69. Sqrt(x)
Implement int sqrt(int x)
.
Compute and return the square root of x, where x is guaranteed to be a non-negative integer.
Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned.
Example 1:
Input: 4
Output: 2
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since
the decimal part is truncated, 2 is returned.
--------------------------------------------------------------------------------------------
这个问题,其实很简单(用sqrt直接OK了),不过,我用二分查找解决这个题
C++代码:
class Solution {
public:
int mySqrt(int x) {
if(x <= ) return x;
int left = ;
int right = x;
while(left <= right){
int mid = left + (right - left)/;
if(x / mid >= mid){
left = mid + ;
}
else
right = mid - ;
}
return right;
}
};
用sqrt:
class Solution {
public:
int mySqrt(int x) {
if(x <= )return x;
return (int)(sqrt(x));
}
};
(二分查找 拓展) leetcode 69. Sqrt(x)的更多相关文章
- (二分查找 拓展) leetcode 162. Find Peak Element && lintcode 75. Find Peak Element
A peak element is an element that is greater than its neighbors. Given an input array nums, where nu ...
- (二分查找 拓展) leetcode 34. Find First and Last Position of Element in Sorted Array && lintcode 61. Search for a Range
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- Leetcode 69. Sqrt(x)及其扩展(有/无精度、二分法、牛顿法)详解
Leetcode 69. Sqrt(x) Easy https://leetcode.com/problems/sqrtx/ Implement int sqrt(int x). Compute an ...
- Leetcode 69 Sqrt(x) 二分查找(二分答案)
可怕的同时考数值溢出和二分的酱油题之一,常在各种小公司的笔试中充当大题来给你好看... 题意很简单,在<二分查找综述>中有描述. 重点:使用简单粗暴的long long来避免溢出,二分均方 ...
- C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】
69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...
- [LeetCode] 69. Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a no ...
- (二分查找 拓展) leetcode278. First Bad Version
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- LeetCode 69. Sqrt(x) (平方根)
Implement int sqrt(int x). Compute and return the square root of x. x is guaranteed to be a non-nega ...
- Leetcode 69. Sqrt(x)
Implement int sqrt(int x). 思路: Binary Search class Solution(object): def mySqrt(self, x): "&quo ...
随机推荐
- ORACLE中如何查找定位表最后DML操作的时间小结
在Oracle数据库中,如何查找,定位一张表最后一次的DML操作的时间呢? 方式有三种,不过都有一些局限性,下面简单的解析.总结一下. 1:使用ORA_ROWSCN伪列获取表最后的DML时间 ORA_ ...
- Boosting Static Representation Robustness for Binary Clone Search against Code Obfuscation and Compiler Optimization(一)
接着上一篇,现在明确问题:在汇编克隆搜索文献中,有四种类型的克隆[15][16][17]:Type1.literally identical(字面相同):Type2.syntactically equ ...
- 根据List集合中的对象属性排序
首先创建一个Student对象,里面有三个属性,分别是int类型,String类型,Date类型 package com.sinoway.cisp.test; import java.text.Sim ...
- django--use
https://docs.djangoproject.com/zh-hans/2.0/intro/
- Configuring Apache Kafka for Performance and Resource Management
Apache Kafka is optimized for small messages. According to benchmarks, the best performance occurs w ...
- zookeeper安装教程(zookeeper3.4.5为例)
zookeeper有单机.伪集群.集群三种部署方式,可根据自己对可靠性的需求选择合适的部署方式.下边对这三种部署方式逐一进行讲解. 一.单机安装 1.1 下载 进入要下载的版本的目录,选择.tar.g ...
- laravel学习笔记一
指定端口 数据迁移 php artisan migrate:install 任何路由 match get,post只选择其一 没有表名对应默认的posts表,如果表为post就不行 时区不对时 分页 ...
- SpringCloud 学习网址记录
SpringCloud Gateway https://www.cnblogs.com/ityouknow/p/10141740.html 熔断降级的概念 https://blog.csdn.net/ ...
- Openwrt自定义CGI实现
此文已由作者吴志勐授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 安装uhttpd. 在编译openwrt前,输入make memuconfig,查找Network -> ...
- Linux和Uboot下eMMC boot分区读写
关键词:eMMC boot.PARTITION_CONFIG.force_ro等. 1. eMMC的分区 大部分eMMC都有类似如下的分区,其中BOOT.RPMB和UDA一般是默认存在的,gpp分区需 ...