Leetcode 二分查找 Search Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
Search Insert Position
Total Accepted: 14279 Total
Submissions: 41575
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
题意:输出一个元素在一个已排序的数组中的位置,假设不存在输出它应该插入的位置
思路:二分查找,假设找到就输出位置。找不到就输出它应该插入的位置
复杂度:时间O(log n),空间O(1)
相关题目:
class Solution {
public:
int searchInsert(int A[], int n, int target) {
int s = 0, e = n , m ; //中间数,偶数个的时候取前一个
while(s < e){
m = s + (e - s) / 2;
if(A[m] == target) return m;
if(A[m] < target) s = m + 1;
if(A[m] > target) e = m;
}
if(A[m] > target) return m;
else return m + 1;
}
};
Leetcode 二分查找 Search Insert Position的更多相关文章
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 【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 ...
- leetcode 二分查找 Search in Rotated Sorted ArrayII
Search in Rotated Sorted Array II Total Accepted: 18500 Total Submissions: 59945My Submissions Follo ...
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- Leetcode No.35 Search Insert Position(c++实现)
1. 题目 1.1 英文题目 Given a sorted array of distinct integers and a target value, return the index if the ...
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- LeetCode OJ 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 Problem 35:Search Insert Position
描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode OJ:Search Insert Position(查找插入位置)
Given a sorted array and a target value, return the index if the target is found. If not, return the ...
随机推荐
- Memento 备忘录 快照模式 MD
备忘录模式 简介 在不破坏封装的前提下,捕获一个对象的[内部状态],并在该对象之外保存这个状态,这样以后就可以将该对象恢复到原先保存的状态. 角色: 发起人Originator:要被备份的成员,它提供 ...
- 转】未指定 INSTANCESHAREDWOWDIR 命令行值。如果指定INSTANCESHAREDDIR 值,则必须指定该值 .
插入光盘后不要用autorun的安装,使用命令行安装!cd进安装光盘的根目录,输入命令:setup.exe /INSTALLSHAREDDIR="D://Program Files//Mic ...
- 关于ListView中getView被重复调用的问题
我用ListView显示数据时,自定义了一个适配器(extends ArrayAdapter),然后重写了getView方法,现在出现一个问题,就是这个getView()方法被重复调用了,比如我的_d ...
- Android -- onWindowFocusChanged
Android中获取手机屏幕的高度和宽度,我们知道在onCreate方法中获取到的值都是为0的,有人说可以在onClick方法中获取值,这个也是个方法 ,但在onWindowFocusChanged方 ...
- mybatis结果的组装(springboot)
文主要解答一个问题,即如果bean没有setter,而且属性不是public的,mybatis的自动组装是否可以赋值成功的问题. 查询调用过程 DefaultSqlSession.selectList ...
- rapidxml 节点加入另一个xml
void TestRapidXml() { ]; sprintf(xmlContent,"<root><head>aaa</head><body&g ...
- Jquery Types 小结
JavaScript provides several built-in(内置的) datatypes. In addition to those, this page documents virtu ...
- Oracle 与 SqlServer 的区别浅析总结
我主要用过的数据库为Oracle10g和SqlServer2008,通过实际运用和查阅资料整理如下: 主题 Oracle 10g SQLServer 2008 存储过程格式 Create Or Rep ...
- 使用Iperf工具测试android系统网络wifi的吞吐量wifithrougput
http://blog.csdn.net/bingxuebage/article/details/7534655 服务端:./iperf3 -s &客户端:./iperf3 -c 10.15. ...
- ls命令(转)
原文:http://www.cnblogs.com/peida/archive/2012/10/23/2734829.html ls命令是linux下最常用的命令.ls命令就是list的缩写缺省下l ...