Leetcode 35 Search Insert Position 二分查找(二分下标)
基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题。。。
题意:在一个有序数列中,要插入数target,找出插入的位置。
楼主在这里更新了《二分查找综述》第一题的解法,比较类似,当然是今天临时写的。
知道了这题就完成了leetcode 4的第二重二分的写法了吧,楼主懒。。。
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int l = , r = nums.size() - ;
while(l <= r){
int mid = l + (r - l)/;
if(nums[mid] < target){
l = mid + ;
}
else r = mid - ;
}
return l;
}
};
这里给出楼主当时A题的解法,请不要鄙视楼主。。。
class Solution {
public:
int searchInsert(vector<int>& nums, int target){
vector<int>::iterator it = lower_bound(nums.begin(), nums.end(), target);
return it - nums.begin();
}
};
是的,又是库函数^_^
Leetcode 35 Search Insert Position 二分查找(二分下标)的更多相关文章
- LeetCode 35 Search Insert Position(查找插入位置)
题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [LeetCode] 35. Search Insert Position 搜索插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- 【LeetCode】- Search Insert Position(查找插入的位置)
[ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...
- [leetcode 35] Search Insert Position
1 题目: Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- [LeetCode] 35. Search Insert Position 解决思路
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- LeetCode 35. Search Insert Position (搜索嵌入的位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- Leetcode 35——Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
- [leetcode]35. Search Insert Position寻找插入位置
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- DIY操作系统(一)
先说几句题外话: 回想第一次看到<30天自制操作系统>这本书时,就被这快餐般的标题深深吸引了,我无法想象如此复杂有内涵的内容能在30天就弄出来,直到我花了一个多月看到这本书的第9天时,我放 ...
- angular.element的常用方法
addClass()-为每个匹配的元素添加指定的样式类名after()-在匹配元素集合中的每个元素后面插入参数所指定的内容,作为其兄弟节点append()-在每个匹配元素里面的末尾处插入参数内容att ...
- 黑马程序员-nil Nil NULL NSNull 野指针和空指针
空指针1.空指针指不含有任何内存地址的指针.在没有具体初始化之前,其被符值为0Dog * dog = nil;Dog * dog = NULL;都为空指针2.野指针指指向的内存为垃圾内存,导致其值不确 ...
- php面向对象学习
近期跟着别人开发一套php程序,深感自己面向对象很白痴,于是再次巩固了一下面向对象的学习,自己整理了一下这几天面向对象的东西,给大家分享!! 面向对象的三大特性: 封装 -- 隐藏内部实现,稳定外部接 ...
- ubuntu10.04下修改mysql的datadir的问题
ubuntu10.04下修改mysql的datadir的问题 转自:http://blog.sina.com.cn/s/blog_4152a9f50100mq5i.html 昨天由于服务器空间告紧,需 ...
- a标签创建超链接,利用a标签创建锚点
#Html今日学习内容 <!DOCTYPE html> <html> <head lang="en"> <meta charset ...
- FreeRTOS和Ucos在打开关闭中断的区别
- SCI Index
Nature.[link] Science.[link] ...
- 2013-08-12【随笔2】-Roy
最近总是因为一下小事情,就会变得闹心,就会自己胡思乱想,自己吓唬自己,自己给自己找烦恼. 是因为每天无所事事,日子过得没有了重点,然后每天人心惶惶,有点杞人忧天了. 还是因为这样平淡的日子,消磨了我们 ...
- python string intern
python 字符串是不可变的. 字符串pool会对 t "looklike" Python identifiers 字符串做intern缓存.