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 ...
随机推荐
- BestCoder Round #27
Jump and Jump... Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Java系统和PHP系统相互调用
一.HTTP JSON方式的缺点 JSON序列化效率低 多语言服务治理功能低 二.关于RPC框架 RPC 框架大致分为两类,一种是偏重服务治理,另一种侧重跨语言调用 2.1 服务治理型 特点 功能丰富 ...
- Codeforces343D(SummerTrainingDay06-F dfs序+线段树)
D. Water Tree time limit per test:4 seconds memory limit per test:256 megabytes input:standard input ...
- python去掉字符串中空格的方法
1.strip():把头和尾的空格去掉 2.lstrip():把左边的空格去掉 3.rstrip():把右边的空格去掉 4.replace('c1','c2'):把字符串里的c1替换成c2.故可以用r ...
- promise 拙见
一,promise是什么? promise 是最早由社区提出和实现是一种解决异步编程的方案,比其他传统的解决方案(回调函数和事件)更合理和强大. ES6 将其写进了语言标准,统一了用法,原生提供了 p ...
- JavaScript 事件处理机制
DOM 事件流 DOM 是一个树型结构,当一个HTML元素产生一个事件时,该事件会在该元素结点与根节点之间按特定的顺序传播,路径所经过的节点都会收到该事件,这个传播过程可称为DOM事件流.而事件传播的 ...
- 【代码笔记】iOS-手机版本号,机型,操作系统版本,设备唯一标识符
一,代码. RootViewController.m #import "ViewController.h" #import "sys/utsname.h" @i ...
- mongose + express 写REST API
一.准备工具 先确保电脑已经安装好nodejs 1.mongoose:安装非常简单: npm install mongoose --save [mongoose封装了mongodb的方法,调用mo ...
- wampserver的配置教程
对于初做PHP网站的朋友来说,第一步肯定是希望在自己电脑是搭建PHP环境,省去空间和上传的麻烦!但搭建环境也不是件容易的事情,特别是对于新手同学来说!因此在这里跟大家介绍我作为一名新手在使用的方便好用 ...
- Oracle 修改oracle数据库名
Oracle 修改oracle数据库名 by:授客 QQ:1033553122 1.确保你有个可用于数据库恢复的,完整的数据库备份 2.确保数据库处于mount,非open状态,并且在加载前先以imm ...