leetCode题解之寻找插入位置
1、问题描述
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.
给定一个排好序的数组和一个数,找出这个数在有序数组中位置。
2、问题分析
采用二分法查找,如果该数在数组中,则返回下标。如果查找结束,该数不在数组中,则返回当前下标 +1
3、代码
int searchInsert(vector<int>& nums, int target) {
if( nums.size() == )
return ;
int left = ,right = nums.size() - ;
while(left <= right)
{
int mid = (left+right) / ;
if( nums[mid] == target)
return mid;
else if( nums[mid] > target)
right = mid -;
else
left = mid + ;
}
return right+;
}
leetCode题解之寻找插入位置的更多相关文章
- leetCode题解之寻找string中最后一个word的长度
1.题目描述 返回一个 string中最后一个单词的长度.单词定义为没有空格的连续的字符,比如 ‘a’,'akkk'. 2.问题分析 从后向前扫描,如果string是以空格‘ ’结尾的,就不用计数, ...
- leetCode题解之寻找一个数在有序数组中的范围Search for a Range
1.问题描述 Given an array of integers sorted in ascending order, find the starting and ending position o ...
- 【LeetCode题解】19_删除链表的倒数第N个节点(Remove-Nth-Node-From-End-of-List)
目录 描述 解法:双指针 思路 Java 实现 Python 实现 复杂度分析 更多 LeetCode 题解笔记可以访问我的 github. 描述 给定一个链表,删除链表的倒数第 n 个节点,并且返回 ...
- [LeetCode 题解] Search in Rotated Sorted Array
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目描述 Suppose an array ...
- [LeetCode 题解]:Intersection of Two Linked Lists
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Suppose an ...
- [LeetCode 题解]: Triangle
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a tr ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- leetcode题解-122买卖股票的最佳时期
题目 leetcode题解-122.买卖股票的最佳时机:https://www.yanbinghu.com/2019/03/14/30893.html 题目详情 给定一个数组,它的第 i 个元素是一支 ...
- 【LeetCode题解】3_无重复字符的最长子串(Longest-Substring-Without-Repeating-Characters)
目录 描述 解法一:暴力枚举法(Time Limit Exceeded) 思路 Java 实现 Python 实现 复杂度分析 解法二:滑动窗口(双指针) 思路 Java 实现 Python 实现 复 ...
随机推荐
- GODADDY 优质DNS 未被墙
下面列出的是我本地测试出的优质服务器 NS10.DOMAINCONTROL.COM NS12.DOMAINCONTROL.COM NS14.DOMAINCONTROL.COM NS19.DOMAINC ...
- django项目的生产环境部署,利用nginx+uwsgi
1.坏境准备 centos6.5 django项目 python坏境(python3.6,) 所需的各种模块(django,uwsgi,sqlite3)具体看坏境 我的测试django项目的数据库用的 ...
- 【Express系列】第2篇——主程序的改造
上一篇对项目的目录结构和 app.js 等一些文件做了一些改造,然而那只是开始. 接下来将做进一步的改造和完善. 我们先看看几个主要的脚本文件,下面的代码是我稍微修改过并添加注释的,方便理解每句代码的 ...
- 修改salt-minion的id后报错解决方法
centos7使用命令 /usr/bin/salt-minion start运行报错 Error parsing configuration file: /etc/salt/master - expe ...
- 使用 Selenium 实现基于 Web 的自动化测试
(转自http://www.ibm.com/developerworks/cn/web/1209_caimin_seleniumweb/index.html) Selenium 是一个用于 Web 应 ...
- async 和 await 的用法示例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 二十一、curator recipes之TreeCache
简介 curator的TreeCache允许对某个路径的数据和路径变更以及其下所有子孙节点的数据和路径变更进行监听. 官方文档:http://curator.apache.org/curator-re ...
- ActiveMQ 报错 Temporary Store limit is 51200 mb
ERROR | Temporary Store limit is 51200 mb, whilst the temporary data directory: D:\tool\apache-actil ...
- 深入理解MyBatis的原理(三):配置文件(上)
前言:前文提到一个入门的demo,从这里开始,会了解深入 MyBatis 的配置,本文讲解 MyBatis 的配置文件的用法. 目录 1.properties 元素 2.设置(settings) 3. ...
- Unity之使用技巧记录
A:写了个死循环Unity无响应崩溃了怎么办? Q::到文件夹里找到刚刚写的脚本,把错误的代码屏蔽掉,再回到Unity