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 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
分析
此题目与上一个题目相似都是源于二分查找的变形。
若序列中存在目标元素值,则直接返回其下标,若不存在则返回第一个大于它的元素的下标。仅仅须要在二分搜索算法中略微改动就可以。
AC代码
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
if (nums.size() == 0)
return 0;
else if (nums.size() == 1)
{
if (nums[0] >= target)
return 0;
else
return 1;
}
else{
return BinarySearch(nums, target);
}
}
int BinarySearch(vector<int> & nums, int target)
{
int left = 0, right = nums.size() - 1;
while (left <= right)
{
int mid = (left + right) / 2;
if (nums[mid] == target)
return mid;
else if (nums[mid] < target)
{
if (mid == right || nums[mid + 1] > target)
return mid + 1;
else
left = mid + 1;
}
else{
if (mid == left || nums[mid - 1] < target)
return mid;
else
right = mid - 1;
}
}//while
return -1;
}
};
LeetCode(35) Search Insert Position的更多相关文章
- [LC]35题 Search Insert Position (搜索插入位置)
①英文题目 Given a sorted array and a target value, return the index if the target is found. If not, retu ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- LeetCode(81) Search in Rotated Array II
题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- LeetCode(74) Search a 2D Matrix
题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...
- LeetCode(34)Search for a Range
题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...
- LeetCode(33)Search in Rotated Sorted Array
题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...
- LeetCode(35):搜索插入位置
Easy! 题目描述: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1 ...
- Leetcode(35)-搜索插入位置
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 这个题目很简单,因为它是给定的排序数组而且没有重 ...
- Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position)
Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...
随机推荐
- 【Linux】shell数组
一.概念 shell数组就是一个元素集合,它把有限个元素用一个名字来命名,然后用编号对他们分区.这个名字称为数组名,用于区分不同内容的编号称为数组的下标. 二.shell数组的定义与增删改查 1.sh ...
- Knockout学习之控制流绑定器
控制流绑定器 “foreach”绑定 顾名思义,通过该绑定我们就可以将监控数组循环输出到页面中去了,当然我们还是先来段简单的示例,仅仅只是输出监控数组: <ul data-bind=" ...
- nmake构建Geos库
1.下载源码包 下载地址 http://download.osgeo.org/geos/geos-3.6.1.tar.bz2 下载之后解压即可. 2.编译 geos源码包中自带了makefile.vc ...
- gnu screen的用法
在使用ssh或者telnet登录远程主机后执行一些耗时的命令, 如果此时ssh或者telnet中断, 那么远程主机上正在执行的程序或者说命令也会被迫终止. screen能够很好地解决这个问题, scr ...
- (原)tensorflow中finetune某些层
转载请注明处处: http://www.cnblogs.com/darkknightzh/p/7608709.html 参考网址: https://kratzert.github.io/2017/02 ...
- Java中用HttpsURLConnection访问Https链接
在web应用交互过程中,有很多场景需要保证通信数据的安全:在前面也有好多篇文章介绍了在Web Service调用过程中用WS-Security来保证接口交互过程的安全性,值得注意的是,该种方式基于的传 ...
- 大家所说的full-stack框架到底是指什么?
轻量级框架: 整合层 guice ORM层 nutz, guzz 表示层 -- None -- JSF Spring MVC ...
- stuff in /proc/sys/fs/
This subdirectory contains specific file system, file handle, inode, dentry and quota information. 1 ...
- Android开发环境——SDK相关内容汇总
Android开发环境将分为SDK相关内容.Eclipse ADT相关内容.模拟器AVD相关内容.调试器DDMS相关内容.日志LogCat相关内容.连接驱动ADB相关内容.内存泄露检测工具MAT相关 ...
- IP概念盛行的背后:资本在狂欢,电影想哭泣 IP,英文“Intellectual Property”的缩写,直译为“知识产权”。它的存在方式很多元,可以是一个故事,也可以是某一个形象,运营成功的IP可以在漫画、小说、电影、玩具、手游等不同的媒介形式中转换。
IP概念盛行的背后:资本在狂欢,电影想哭泣 IP容易拉投资.谈合作,甚至还能简化宣发途径,越来越多的人涌入了电影这个产业,争抢IP成为他们进入行业的最快捷的方法.IP盛行暴露出的另一个问题是国产电影原 ...