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 ...
随机推荐
- 设置代码Code高亮显示成蓝色
下面方法是让设置的关键字高亮显示,考虑到了注释与字符串的影响,所以备用,以便将来能够用到. private static void ColorizeCode(RichTextBox rtb) { st ...
- Python 5 —— OOP
OOP class MyClass: y = None def __init__(self,x,y): self.__x = x self.y = y def getx(self): return s ...
- 8.4.3 Glide
1). 导入库 dependencies { compile 'com.github.bumptech.glide:glide:3.5.2' compile 'com.android.support: ...
- PXE网络启动提示no default or ui configuration directive问题解决
按照 https://help.ubuntu.com/community/DisklessUbuntuHowto 的提示配置完系统,准备网络启动的时候,遇到: Trying to load pxeli ...
- Ubuntu下使用USB串口
Ubuntu本身一般都带了USB转串口的驱动. 1. 首先确认系统支持USBSerial,输入以下命令: lsmod | grep usbserial 2. 接上USB串口线,看看系统是否可 ...
- Windows下mysql忘记root密码的解决方法
1. 首先检查mysql服务是否启动,若已启动则先将其停止服务,可在开始菜单的运行,使用命令: net stop mysql 打开第一个cmd窗口,切换到mysql的bin目录,运行命令: mysql ...
- 纯练手设置ip地址脚本
#!/bin/bash IFO() { read -p "Configure $line Network card ( 'yes'or'no' )?" CDN </dev/t ...
- DirectXMath
DirectXMath 是Windows平台下的数学库,DirectXMath 库基于 XNA Math C++ SIMD library version 2.04 . DirectXMath 库 ...
- 利用HtmlAgilityPack库进行HTML数据抓取
主要介绍基于XPATH的文本分析方式的实现,代码如下: using System; using System.Collections.Generic; using System.Linq; using ...
- myrocks之事务处理
前言 mysql目前支持的事务引擎有innodb,tokudb. rocksdb加入mysql阵营后,mysql支持的事务引擎增长至3个.myrocks目前支持的事务隔离级别有read-committ ...