LeetCode第[69]题(Java):Sqrt(x)
题目:求平方根
难度:Easy
题目内容:
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.
翻译:
计算并返回x的平方根,其中x保证是一个非负整数。
由于退货类型是一个整数,所以小数部分被截断,并且只返回结果的整数部分。
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.
我的思路:一个正整数(除了0和1)的平方根一定是从1到x/2,之间的某一个数,所以就相当于查找算法,所以采用二分法即可
需要注意的是,我们的寻找条件,
如果mid * mid > x right = mid - 1;
否则:此时mid * mid <= x,还满足,(mid+1) * (mid+1)> x 此时mid即可返回
否则:left = mid +1;
我的代码:
public int mySqrt(int x) {
if (x <= 1)
return x;
int left = 1, right = x/2;
while (true) {
int mid = left + (right - left)/2;
if (mid > x/mid) {
right = mid - 1;
} else {
if (mid + 1 > x/(mid + 1))
return mid;
left = mid + 1;
}
}
}
我的复杂度:O(n)
编程过程中的问题:
1、因为mid在判断中也是判断过了的,不像Search in Rotated Sorted Array那样是等循环结束,所以left和right的下一个值都不需要再包括mid
答案代码:和我的一样。
LeetCode第[69]题(Java):Sqrt(x)的更多相关文章
- LeetCode第[18]题(Java):4Sum 标签:Array
题目难度:Medium 题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + ...
- LeetCode第[1]题(Java):Two Sum 标签:Array
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[46]题(Java):Permutations(求所有全排列) 含扩展——第[47]题Permutations 2
题目:求所有全排列 难度:Medium 题目内容: Given a collection of distinct integers, return all possible permutations. ...
- LeetCode第[1]题(Java):Two Sum (俩数和为目标数的下标)——EASY
题目: Given an array of integers, return indices of the two numbers such that they add up to a specifi ...
- LeetCode第[11]题(Java):Container With Most Water 标签:Array
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays 标签:Array
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
- LeetCode第[29]题(Java):Divide Two Integers
题目:两整数相除 难度:Medium 题目内容: Given two integers dividend and divisor, divide two integers without using ...
- LeetCode第[11]题(Java):Container With Most Water (数组容器盛水)——Medium
题目难度:Medium Given n non-negative integers a1, a2, ..., an, where each represents a point at coordina ...
- LeetCode第[4]题(Java):Median of Two Sorted Arrays (俩已排序数组求中位数)——HARD
题目难度:hard There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median ...
随机推荐
- finereport---FineReport入门常见疑难点
一.入门介绍 二.入门需知 注意:开发人员可以设置DEBUG级别,有助于测试 三.数据准备 数据集sql中可以使用参数宏${}动态地生成过滤条件,${}中的语句在FineReport报表中执行,将${ ...
- mysql 中调用存储过程之后,连接断开不可用
解决方法: 由 mysql_real_connect(&m_mysql,host,user,passwd,Db,0,NULL,0) == NULL 改为 mysql_real_connect( ...
- c++相关知识
0.C语言基础知识及系统相关:http://c.biancheng.net/cpp/u/jiaocheng/ 1.C++ include观点与机制:http://developer.51cto.com ...
- SpringMVC 之拦截器和异常处理
1. 文件上传 Spring 使用 Jakarta Commons FileUpload 技术实现了一个 MultipartResolver 实现类: CommonsMultipartResolver ...
- JMH 性能测试框架
参考 1 Java 并发编程笔记:JMH 性能测试框架 http://blog.dyngr.com/blog/2016/10/29/introduction-of-jmh/ 2 Code Samp ...
- 转!!SpringMVC与Struts2区别与比较总结
1.Struts2是类级别的拦截, 一个类对应一个request上下文,SpringMVC是方法级别的拦截,一个方法对应一个request上下文,而方法同时又跟一个url对应,所以说从架构本身上Spr ...
- PyMongo的使用(转)
原文:http://www.oschina.net/code/snippet_1382328_37407 #!/usr/bin/env python #coding:utf-8 # Author: - ...
- mysql 中sum (if()) 用法
原表: id fenlei time 1 分类1 20130316 2 分类2 20130316 3 分类3 20130317 ...
- DBA学习参考绝佳资料
原文来自:pursuer.chen 原文地址:https://www.cnblogs.com/chenmh/default.aspx?page=1 [置顶]MongoDB 文章目录 2018-02-0 ...
- ionic真机调试Android报错 - could not read ok from ADB Server * failed to start daemon * error: cannot connect to daemon
在使用真机调试Android程序时,报错如下: could not read ok from ADB Server * failed to start daemon error: cannot con ...