leetcode25—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.
Example 1:
Input: [1,3,5,6], 5 Output: 2
Example 2:
Input: [1,3,5,6], 2 Output: 1
Example 3:
Input: [1,3,5,6], 7 Output: 4
Example 4:
Input: [1,3,5,6], 0 Output: 0
想法:使用二分查找法,找出target应该在的位置。
class Solution { public: int searchInsert(vector<int>& nums, int target) { == nums.size()){ ; } )) ; if(target > nums.back()){ return nums.size(); } ; ; ){ ; if(nums.at(mid) == target) return mid; if(nums.at(mid) < target){ left = mid; }else{ right = mid; } } return right; } };
leetcode25—Search Insert Position的更多相关文章
- 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 ...
- Leetcode35 Search Insert Position 解题思路(python)
本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 35. Search Inse ...
- 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-algorithms-35 Search Insert Position
leetcode-algorithms-35 Search Insert Position Given a sorted array and a target value, return the in ...
- LeetCode: Search Insert Position 解题报告
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- 【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 Insert Position
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 T ...
随机推荐
- Java 与C++区别:复写(override)
C++中子类复写父类virtual方法要做到函数参数和返回值类型都一致,而Java中返回值类型可以不同,即子类复写的方法返回值类型可以使父类方法返回值类型的一个子类型.比如 返回类型兼容情况 Java ...
- Java 基础知识总结 3
13.java类集 类集实际上是一个动态的对象数组,与一般的对象数组不同,类集中的对象内容可以任意扩充. 类集的特征: 1)这种框架是高性能的 2)框架必须允许不同类型的类集以相同的方式和高度互操作方 ...
- javascript:原型与原型链
一,函数对象 所有引用类型(函数,数组,对象)都拥有__proto__属性(隐式原型) 所有函数拥有prototype属性(显式原型)(仅限函数) 原型对象:拥有prototype属性的对象,在定义函 ...
- JS里面的懒加载(lazyload)
懒加载技术(简称lazyload)并不是新技术, 它是js程序员对网页性能优化的一种方案.lazyload的核心是按需加载 涉及到图片,falsh资源 , iframe, 网页编辑器(类似FCK)等占 ...
- MaxScript与外部程序通讯
最近项目要求通过java给max发送任务指令,max接收指令执行任务,并且返回执行的结果.不管为什么会有这样的需求,有就要去实现. 1.OLE开启 Max本身提供了一个方式,它可以将自己注册成一个Ol ...
- Expo大作战(二十)--expo中的Release channels(不重要*)
简要:本系列文章讲会对expo进行全面的介绍,本人从2017年6月份接触expo以来,对expo的研究断断续续,一路走来将近10个月,废话不多说,接下来你看到内容,讲全部来与官网 我猜去全部机翻+个人 ...
- 第二篇 windows container 微软的原生容器
先上图,显示windows container的体积: 以下是我使用docker pull 命令下载后,又用命令保存到本地的,相对于linux container体积依然巨大无比:据官方新闻,微软原生 ...
- nodejs设置NODE_ENV环境变量(1)
看下app.js文件中的一部分代码,如下: //开发环境错误处理 // will print stacktrace if (app.get('env') === 'development') { ap ...
- 【转】python version 2.7 required,which was not found in the registry
源地址:http://www.cnblogs.com/thinksasa/archive/2013/08/26/3283695.html 方法:新建一个register.py 文件,把一下代码贴进去, ...
- TLS 1.0协议
TLS1.0 协议发布于1999年初.该协议可在Internet中提供给通信双方一条私有信道,即对通信消息进行加密.该协议主要描述了通信密钥协商的方法与通信格式的定义.分别由TLS Handshake ...