leetcode解题报告(11):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
最简单的题目,没有之一
代码如下:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
for(int i = 0; i != nums.size(); ++i){
if(target > nums[i]) continue;
else return i;
}
return nums.size();
}
};
更优雅的解法是二分查找:
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
int low = 0,high = nums.size() - 1;
while(low <= high){
int mid = low + (high - low) / 2;
if(nums[mid] < target)low = mid + 1;
else high = mid - 1;
}
return low;
}
};
leetcode解题报告(11):Search Insert Position的更多相关文章
- LeetCode练题——35. Search Insert Position
1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...
- Leetcode 题目整理 Sqrt && Search Insert Position
Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...
- LeetCode Arrary Easy 35. Search Insert Position 题解
Description Given a sorted array and a target value, return the index if the target is found. If not ...
- LeetCode(35) Search Insert Position
题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...
- LeetCode记录之35——Search Insert Position
这道题难度较低,没有必要作说明. Given a sorted array and a target value, return the index if the target is found. I ...
- LeetCode: Search Insert Position 解题报告
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 ...
随机推荐
- 位带操作—GPIO输出和输入
GPIOC->ODR |=(0<<2); // 总线操作,即操作整个寄存器. 在51单片机中 P0=0xFE; //总线操作. sbit LED1=P0^0; //位操作,即 ...
- c++学习---const 和 string
当在两个文件定义了同名的const变量时,相对于定义了两个独立的变量 想要在一个文件中定义一个const变量并在其他文件中使用他:不管时声明还是定义,都加上extern关键字 因为const对象一经初 ...
- 剑指Offer(4)——替换空格
题目: 请实现一个函数,把字符串中的每个空格替换成"%20".例如输入“We are happy.”,则输出“We%20are%20happy.”. 思路: 如果按照顺序从前往后依 ...
- 103 保序回归 isotonic regression
103 保序回归 isotonic regression 2016-03-30 11:25:27 bea_tree 阅读数 6895 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权 ...
- django中使用redis保存session(转)
原文:https://blog.csdn.net/Enjolras_fuu/article/details/79661582 Django-redis-session官网:https://pypi.o ...
- 怎样给回调函数绑定this
在三种绑定this的方法中, Function.prototype.call() 和 Function.prototye.apply() 都是会立即执行该函数的, 但回调函数是不能立即执行的, 它只是 ...
- Go part 1 初探
Go 语言简介 Go 语言是 Google 在2007年开发的一种开源编程语言,于2009年11月10日向全球公布 出自 Ken Thompson 和 Rob Pike.Robert Grieseme ...
- spring 实现事务配置的方式
spring 中常用的两种事务配置方式以及事务的传播性.隔离级别 一.注解式事务 1.注解式事务在平时的开发中使用的挺多,工作的两个公司中看到很多项目使用了这种方式,下面看看具体的配置demo. 2. ...
- tornado基本使用一
一.tornado web程序编写思路 . 创建web应用实例对象,第一个初始化参数为路由映射列表 . 定义实现路由映射列表中的handler类 . 创建服务器实例, 绑定服务器端口 . 启动当前线程 ...
- elasticsearch + kibana + x-pack + logstash_集群部署安装
elasticsearch 部分总体描述: 1.elasticsearch 的概念及特点.概念:elasticsearch 是一个基于 lucene 的搜索服务器.lucene 是全文搜索的一个框架. ...