[LeetCode] 704. Binary Search
Description
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a function to search target in nums. If target exists, then return its index, otherwise return -1.
Example 1:
Input: nums = [-1,0,3,5,9,12], target = 9
Output: 4
Explanation: 9 exists in nums and its index is 4
Example 2:
Input: nums = [-1,0,3,5,9,12], target = 2
Output: -1
Explanation: 2 does not exist in nums so return -1
Note:
- You may assume that all elements in
numsare unique. - n will be in the range
[1, 10000]. - The value of each element in
numswill be in the range[-9999, 9999].
Analyse
从一个list里找出一个数,不存在则返回-1
使用二分查找,每次将问题的规模减半
int search(vector<int>& nums, int target)
{
int left = 0;
int right = nums.size() - 1;
int mid = (left + right) / 2;
while (left <= right)
{
if (nums[mid] == target)
{
return mid;
}
else if (nums[mid] > target)
{
right = mid - 1;
}
else if (nums[mid] < target)
{
left = mid + 1;
}
mid = (left + right) / 2;
}
return -1;
}
leetcode中最快的方法是调用STL中的lower_bound函数
template <class ForwardIterator, class T>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val);
lower_bound在[first, last)的左闭右开区间寻找第一个不小于val的元素,采用了二分查找的思想
Returns an iterator pointing to the first element in the range [first,last) which does not compare less than val.
在这个区间找到满足条件的就返回指向这个值的iterator, 否则返回last,
An iterator to the lower bound of val in the range.
If all the element in the range compare less than val, the function returns last.
由于lower_bound是左闭右开的搜索,最后一个值未覆盖到,好在如果没找到回直接返回last,对lower_bound返回的值判断一下是否是target就可以覆盖对最后一个元素的搜索
int search(vector<int>& nums, int target)
{
static int fast_io = []() { std::ios::sync_with_stdio(false); cin.tie(nullptr);
return 0; }();
auto it = lower_bound(nums.begin(),nums.end(),target);
return (it!=nums.end() && *it==target) ? it-nums.begin() : -1; //这里解决了val出现在最后的产生的问题
}
Reference
[LeetCode] 704. Binary Search的更多相关文章
- 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 (二分查找)
题目标签:Binary Search 很标准的一个二分查找,具体看code. Java Solution: Runtime: 0 ms, faster than 100 % Memory Usage ...
- 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 ...
- LeetCode: Validata Binary Search Tree
LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search ...
- 【Leetcode_easy】704. Binary Search
problem 704. Binary Search solution: class Solution { public: int search(vector<int>& nums ...
- [LeetCode] 704. Binary Search_Easy tag: Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
随机推荐
- JMeter简介及使用JMeter来访问网站
参考: http://jmeter.apache.org/ http://blog.chinaunix.net/uid-26884465-id-3416869.html http://www.ltes ...
- hdu-6601 Keen On Everything But Triangle
题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=6601 Description N sticks are arranged in a row, and ...
- URAL-1982-Electrification Plan最小生成树或并查集
Electrification Plan 题意:在一个无向图中,给你几个源点,找出把所有点连接到源点后最小的消费: 可以利用并查集: 先用结构体把每个边存起来,再按照消费大小排序.之后从消费小的到大的 ...
- 牛客小白月赛4 B 博弈论 思维 字符串
链接:https://www.nowcoder.com/acm/contest/134/B来源:牛客网 题目描述 铁子和顺溜在学习了博弈论的sg函数之后,解决了很多很多博弈题,现在他们遇到了一道难题. ...
- yzoj P2044 数字游戏 题解
题意 dfs骗了30分,一开始想的距离正解差一点啊,贪心加dp就可以过的水题,真正太蒻了 解析 代码 #include<bits/stdc++.h> using namespace std ...
- Vue中如何使用less
最近发现好多小伙伴在面试的过程中会问到vue如何使用less和scss,所以我绝对更新.复习一下less:废话不多说直接进主题: 依赖下载 1.首先使用npm下载依赖: npm install --s ...
- Java 中初始化 List 集合的 7 种方式
1.常规方式 List<String> languages = new ArrayList<>(); languages.add("Java"); lang ...
- 获取不到jdbc.driver的值解决办法
我存在的问题是: 1.先检查自己是否出错 ①首先想到mysql版本和驱动版本之间的冲突问题,我的mysql是5.5.56,驱动用的5.1.32,上网查了一下可以用,但还是尝试换了一个版本的驱动,还是出 ...
- win7 安装mysql5.7
Windows 64 位 mysql 5.7以上版本包解压中没有data目录和my-default.ini以及服务无法启动的解决办法以及修改初始密码的方法 LZ初学SQL,本来以为开源的安装很简单,但 ...
- 大数据平台搭建 - cdh5.11.1 - hbase集群搭建
一.简介 HBase是一种构建在HDFS之上的分布式.面向列的存储系统.在需要实时读写.随机访问超大规模数据集时,可以使用HBase. 尽管已经有许多数据存储和访问的策略和实现方法,但事实上大多数解决 ...