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 ...
随机推荐
- allegro中焊盘的设置
用Cadence的pad designer制作pad的时候会遇到为thermal relief和anti pad设计尺寸的问题 Thermal relief:正规的中文翻译应该叫做防散热PAD.它主要 ...
- 使用Xcode6创建EmptyProject
多年不写文章,今天突然准备好好写一些博客,以记录自己在编程这条道路上的成长,与所学所悟.提起笔来,才发现,自己的语言之匮乏,思虑再三,始觉不顺.也罢,从头开始慢慢训练吧. 自Xcode6更新之后,默认 ...
- [Leetcode][JAVA] Insert Interval
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessa ...
- python socket server源码学习
原文请见:http://www.cnblogs.com/wupeiqi/articles/5040823.html 这里就是自己简单整理一下: #!/usr/bin/env python # -*- ...
- xshell传输文件到Centos
Reference: [1] http://www.myhack58.com/Article/sort099/sort0102/2015/61154.htm [2] http://www.cnblog ...
- .NET环境下上传和下载Word文件
一.上传Word文档或者其他文档 1.简单地上传文件的web服务方法如下 [WebMethod] public void UploadFile() { using (TransactionScope ...
- 【设计模式之单例模式InJava】
1. 单例模式 1.1饿汉式(开发常用) class SingleFirst { /* 添加其他成员信息 */ private static SingleFirst s1 = new SingleFi ...
- lxc on centos
终于把lxc的网络配通了,也不知道对不对,记一下 一开始都是雷同的地方 yum install libcgroup lxc lxc-templates 安装lxc cgroup 然后记得 chkcon ...
- 快速学习C语言三: 开发环境, VIM配置, TCP基础,Linux开发基础,Socket开发基础
上次学了一些C开发相关的工具,这次再配置一下VIM,让开发过程更爽一些. 另外再学一些linux下网络开发的基础,好多人学C也是为了做网络开发. 开发环境 首先得有个Linux环境,有时候家里机器是W ...
- 【源码分享】WPF漂亮界面框架实现原理分析及源码分享
1 源码下载 2 OSGi.NET插件应用架构概述 3 漂亮界面框架原理概述 4 漂亮界面框架实现 4.1 主程序 4.2 主程序与插件的通讯 4.2.1 主程序获取插件注册的服务 4.2 ...