LeetCode 704. Binary Search (二分查找)
题目标签:Binary Search
很标准的一个二分查找,具体看code。
Java Solution:
Runtime: 0 ms, faster than 100 %
Memory Usage: 39 MB, less than 90 %
完成日期:07/31/2019
关键点:二分查找
class Solution {
public int search(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(nums[mid] == target)
return mid;
else if(nums[mid] < target) // if mid is less than target, move to right half
left = mid + 1;
else // if mid is greater than target, move to left half
right = mid - 1;
}
return -1;
}
}
参考资料:n/a
LeetCode 题目列表 - LeetCode Questions List
题目来源:https://leetcode.com/
LeetCode 704. Binary Search (二分查找)的更多相关文章
- [01]Binary Search二分查找
Binary Search二分查找 作用:二分查找适用于有序的的数组或列表中,如果列表及数组中有n个元素,通过二分查找查询某一元素的位置需要的步骤是log2(n)(注:该log的底数是2) 1.Pyt ...
- leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version
704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...
- [LeetCode] 704. Binary Search
Description Given a sorted (in ascending order) integer array nums of n elements and a target value, ...
- lintcode:Binary Search 二分查找
题目: 二分查找 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. 样例 ...
- STL模板整理 Binary search(二分查找)
前言: 之前做题二分都是手动二分造轮子,用起来总是差强人意,后来看到STL才发现前辈们早就把轮子造好了,不得不说比自己手动实现好多了. 常用操作 1.头文件 #include <algorith ...
- 【算法模板】Binary Search 二分查找
模板:(通用模板,推荐) 给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1. ...
- Leetcode704.Binary Search二分查找
给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1. 示例 1: 输入: num ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- LeetCode:Unique Binary Search Trees I II
LeetCode:Unique Binary Search Trees Given n, how many structurally unique BST's (binary search trees ...
随机推荐
- Linux下常用的配置文件位置
1.别名配置文件 [root@room8pc205 ~]# vim /root/.bashrc #此处是root用户定义的别名文件的位置,只有root用户登录可用 [root@room8pc2 ...
- SQL语句常用优化技巧
提高SQL语句的执行效率,最常见的方法就是建立索引,以及尽量避免全表扫描. ①.避免在where子句中使用 is null 或 is not null 对字段进行判断. 如:select id fro ...
- Linux环境进程间通信----信号与管道
一.信号: 信号主要用来通知进程异步事件的发生.可以使用“kill -l ”命令来显示系统中的信号.进程可以忽略大部分信号,但是有两个是不能忽略的: (1)SIGSTOP:这个信号将中断进程的执行. ...
- Codeforces gym102222 B.Rolling The Polygon 凸包/余弦定理
题意: 有一个不保证凸的多边形,让你滚一圈,计算某点滚出的轨迹多长. 题解: 求出凸包后,以每个点为转轴,转轴到定点的距离为半径,用余弦定理计算圆心角,计算弧长. #include<iostre ...
- html+css判断各个IE浏览器版本
html+css判断各个IE浏览器版本 在编写网页代码时,各种浏览器的兼容性是个必须考虑的问题,有些时候无法找到适合所有浏览器的写法,就只能写根据浏览器种类区别的代码,这时就要用到判断代码了. 在HT ...
- (8)centos7 登录与关机
关机重启 shutdown -h now #立刻关机 shutdown -h 1 #一分钟后关机 shutdown -h 17:00 #指定时间关机(如果当前时间超过17:00,则会转到明天的17:0 ...
- python re.findall 使用
python re.findall 使用 import re #\w 匹配字母数字及下划线 print(re.findall('\w','hello alan _god !@^&#^$^!*& ...
- 我看Spring MVC系列(一)
1.Spring MVC是什么: Spring MVC:Spring框架提供了构建Web应用程序的全功能MVC模块. 2.Spring helloWorld应用(基于Spring 4.2) 1.添加S ...
- 8.1 图像API
8.1 图像API Routine Description Drawing related functions GUI_AddRect() 调整矩形框的大小 GUI_GetClientRect() R ...
- Flink DataStream API
Data Sources 源是程序读取输入数据的位置.可以使用 StreamExecutionEnvironment.addSource(sourceFunction) 将源添加到程序.Flink 有 ...