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 ...
随机推荐
- PHP ftp_pwd() 函数
定义和用法 ftp_pwd() 函数返回指定 FTP 连接的当前目录名称. 语法 ftp_pwd(ftp_connection) 参数 描述 ftp_connection 必需.规定要使用的 FTP ...
- 预处理+状态压缩+剪枝——codefoece 1209E
那一步剪枝实在是没想到 #include<bits/stdc++.h> using namespace std; #define N 2005 struct Col{ ],Max[< ...
- mysql|tomcat|nginx|redis在docker中的部署
MySQL部署 拉取MySQL镜像 docker pull mysql 查看镜像 创建MySQL容器 docker run -di --name pinyougou_mysql -p 33306:33 ...
- [nRF51822 AK II 教程]第一课,开发环境的配置及背景介绍【转】
低功耗蓝牙4.0是全新的技术,并不向下兼容,也就是说它和蓝牙3.0.2.0什么的都不能通信的.另外,蓝牙4.0目前的规范只能做外设和主机(智能手机,电脑等)通讯,也就是说你想用一个单模的蓝牙4.0开发 ...
- 服务器安装TeamViewer 13
服务器安装TeamViewer 13 服务器上安装TeamViewer,网上找了个教程开始安装,里面有坑,安装时要根据自己的情况而定.Linux系统更新太快,网上教程可能会有一些出入. TeamVie ...
- jeecg随笔 -- 实体关联属性的设计
转载:https://www.iteye.com/blog/1868620 在jeecg 里 ,是根据数据库生成实体的,很多关联关系需要自己进行进一步整理才能满足我们的业务需求 例如外键关系 由于很多 ...
- Codeforces 1119E Pavel and Triangles (贪心)
Codeforces Global Round 2 题目链接: E. Pavel and Triangles Pavel has several sticks with lengths equal t ...
- Java目录事件
当文件系统中的对象被修改时,我们可以监听watch服务以获取警报.java.nio.file包中的以下类和接口提供watch服务. Watchable接口 WatchService接口 WatchKe ...
- c程序查找字符出现次数
#include <stdio.h> int main(){ char str[100],ch,M,Empty; int i, frequency = 0; fgets(str, (siz ...
- leetcode.字符串.205同构字符串-Java
1. 具体题目 给定两个字符串 s 和 t,判断它们是否是同构的.如果 s 中的字符可以被替换得到 t ,那么这两个字符串是同构的.所有出现的字符都必须用另一个字符替换,同时保留字符的顺序.两个字符不 ...