35. Search Insert Position (Array; Divide-and-Conquer)
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6], 5 → 2
[1,3,5,6], 2 → 1
[1,3,5,6], 7 → 4
[1,3,5,6], 0 → 0
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if(nums.size()==) return ;
return binarySearch(nums,,nums.size()-,target);
}
int binarySearch(vector<int>& nums, int start, int end, int target){
if(start==end){
if(target <= nums[start]) return start;
else return start+;
}
int mid = start + ((end-start)>>);
if(target <= nums[mid]) return binarySearch(nums,start,mid,target);
else return binarySearch(nums,mid+,end,target);
}
};
35. Search Insert Position (Array; Divide-and-Conquer)的更多相关文章
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- 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】35. Search Insert Position (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 35. Search Insert Position@python
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
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 ...
随机推荐
- caddy server 几个常用插件
1.log日志 log /var/www/log/example.log 2.目录访问 browse 3.gzip压缩 gzip 4.自主ssl证书 tls /path/ssl/exa ...
- input type=number 禁止输入字符“e”的办法
输入框input,的type设置为number,本想只输入数字,但是字符“e”却能通过, 首先科普一下, <body> <input onkeypress="getCode ...
- 队列模拟题——pat1026. Table Tennis
题意自己理解了,主要是两个队列维护,一个VIP队列,一个普通队列 搜集了一些坑(有些坑转自别的网站用于广大同学的测试之用) 普通人也有VIP的权益!!! 屌丝逆袭有木有!!! 920:52:00 10 ...
- 【备忘录】Sublime Text编辑器如何在选中的多行行首增加字符串
如题:上面的代码,想在每一行的开头加上一个字符 * 如下: 操作步骤如下: 1.选中要操作的行(我这里Ctrl+A) 2.Ctrl+Shift+L (待操作状态) 3.方向键← (操作这步骤后,可 ...
- python3之es+log+date+timezone
from dateutil.parser import parse # 使用它可以方便的将字符串解析为datetimefrom tzlocal import get_localzone # 使用它可以 ...
- OPC UA (统一架构)的优势
OPC UA OPC统一架构(OPC Unified Architecture)是OPC基金会(OPC Foundation)创建的新技术,更加安全.可靠.中性(与供应商无关),为制造现场到生产计划或 ...
- couchdb的使用例子
couchdb安装 sudo apt-get install erlang sudo apt-get install libmozjs185-dev libicu-dev 下载源码,编译安装 启动以后 ...
- linux保存的设置用户/组ID(set-user-ID)的测试
直接贴代码和结果 // FileName: id.cpp #include <iostream> #include <unistd.h> using namespace std ...
- 20165226 2017-2018-4 《Java程序设计》第7周学习总结
20165226 2017-2018-4 <Java程序设计>第7周学习总结 教材学习内容总结 第十一章 JDBC与MySQL数据库 下载MySQL最新版本. 连接数据库 下载JDBC-M ...
- angula的factory service provider
本人学了一段时间的angular的服务(factory.service.provider),有了自己的一些对于他们的见解,如果说的对,敬请赐教!!! 以后更新