【Leetcode】【Medium】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
解题思路:
典型的二分查找应用,注意数组下标为0~n-1,但是可能数字需要插在数组最后第n的位置上。
由于需要找到一个大于等于target数所在的位置,进行插入;所以当nums[mid] > target时,r应该等于mid;因此采用 mid=(l+r)/2,l=mid+1的组合;
对于二分查找有兴趣的同学,可以看另一道Leetcode题目Search for a Range,对二分查找的应用更加全面,在里面,我也写了一些自己学习二分查找总结的心得;
代码:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int l = ;
int r = nums.size();
while (l < r) {
int mid = (l + r) / ;
if (nums[mid] > target)
r = mid;
else if (nums[mid] == target)
return mid;
else
l = mid + ;
}
return r;
}
};
【Leetcode】【Medium】Search Insert Position的更多相关文章
- 【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 (2 solutions)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 60. Search Insert Position 【easy】
60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...
- 【LeetCode题意分析&解答】40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 【LeetCode题意分析&解答】37. Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells. Empty cells are indicated by th ...
- [LeetCode] 035. Search Insert Position (Medium) (C++)
索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Sea ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- [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)
Leetcode之二分法专题-35. 搜索插入位置(Search Insert Position) 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会 ...
随机推荐
- Java 数组实现堆栈操作
class Stack { private int stck[] ; private int tos ; Stack(int size) { // 一个参数的构造参数 stck = new int[s ...
- Unity中利用委托与监听解耦合的思路
这篇随笔是一篇记录性的随笔,记录了从http://www.sikiedu.com/my/course/304,这门课程中学到的内容,附带了一些自己的思考. 一.单例模式的应用 首先假想一种情况,现在需 ...
- 截取字符串-【.net】
问题: 如: asdasdfasdf asdfasdf 12sdfsdf asdfa 截取后为: asdasdfasdf asdfasdf 12sdfsdf 为第一段asdfa 为第二段 解答: Di ...
- eclipspe导入hibernate的源代码
看图:
- Winform取消用户按下键盘的事件
1. 有时候针对某个控件,想取消它的所有键盘按下事件, 只需要为这个控件绑定keyDown事件即可,然后处理的代码如下: private void txtDeviceName_KeyDown(obje ...
- Bootstrap使用模态框modal实现表单提交弹出框
Bootstrap 模态框(Modal)插件 模态框(Modal)是覆盖在父窗体上的子窗体.通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动.子窗体可提供信息.交互等.如果 ...
- php对图片加水印--将文字作为水印加到图片
方法代码: /** * 图片加水印(适用于png/jpg/gif格式) * * @author flynetcn * * @param $srcImg 原图片 * @param $wat ...
- [转].NET Core dotnet 命令大全
本文转自:http://www.cnblogs.com/linezero/p/dotnet.html https://docs.microsoft.com/en-us/dotnet/articles/ ...
- MVVMLight - IOC Containers and MVVM
在面向对象编程的早期,开发者要面对在应用程序或者类库中创建或检索类的实例的问题.针对这个问题有很多的解决方案.在过去几年中,依赖注入(DI)和控制反转(IoC)在开发者中很流行,并且取代了老的方案,比 ...
- android socket 通讯(客户端) 发送数据
/** ClientSocket通讯类 **/ public class ClientSocket { /**服务器地址*/ private String serverUrl=&q ...