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) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...
随机推荐
- 5、redis之使用spring集成commons-pool
添加spring的依赖 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...
- rabbitmq重装依赖的erlang 要注意
今天安装的erlang和rabbitmq版本不匹配导致出现各种问题,在使用正确版本安装后出现问题,在日志中找到报错信息: {"init terminating in do_boot" ...
- Ceontos6.X配置XDMCP远程调用图形化
Linux一般不需要图形化,但是偶尔也是需要的,基于X11协议的图形化XDMCP很受推广,VNC也是很不错的... 前提: 关闭防火墙,不关闭需要配置177端口放行 关闭selinux,不关闭自己配置 ...
- NRF24L01无线模块的使用
NRF2401芯片pin定义 NRF24L01模块pin定义 VCC 脚接电压范围为 1.9V~3.6V 之间, 不能在这个区间之外, 超过 3.6V 将会烧毁模块, 推荐电压 3.3V 左右 除电源 ...
- ios中layoutsubview何时被调用
layoutsubview和viewDidlayoutsubview(控制器)被调用的集中情况 一:当view的frame或bounds发生改变 1:直接改view的frame或bounds 会调用v ...
- CentOS SVN客户端应用
一.CentOS安装TortoiseSVN yum install -y subversion 二.SVN客户端命令 1.将文件checkout到本地目录 svn checkout path( ...
- hihocoder第229周:最大连续字母个数
题目链接 给定一个仅包含小写字母的字符串s(长度小于1e5),你可以交换任意两个字符的位置,现在允许交换k次,要求交换之后,s中最长的连续相同字符个数尽量多,求这个最长连续区间的长度. 样例 输入 1 ...
- bat批处理,实现获取目录
@echo off echo 当前盘符:%~d0 echo 当前盘符和路径:%~dp0 echo 当前批处理全路径:%~f0 echo 当前盘符和路径的短文件名格式:%~sdp0 echo 当 ...
- 【Spring】SpringMVC中浅析数据的传递方式
包括了基本数据类型的传递和 Date数据类型的传递.关于SpringMVC的配置可以参见基于注解实现SpringMVC+MySQL 假设有表单页面如下: <h1>登录</h1> ...
- class-dump在osx 10.11以后安装方法
当Mac升级了OSX 10.11后,配置class-dump的时候,会发现逆向书上推荐的class-dump存放目录/usr/bin,class-dump存放不进去,尝试过用sudo 还是不被允许 ...