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 实现 复 ...
随机推荐
- javascript双等号引起的类型转换
隐性类型转换步骤 一.首先看双等号前后有没有NaN,如果存在NaN,一律返回false. 二.再看双等号前后有没有布尔,有布尔就将布尔转换为数字.(false是0,true是1) 三.接着看双等号前后 ...
- 两个字符串 char* a, char* b,输出b在a中的位置次序。
/** 题目: 两个字符串 char* a, char* b,输出b在a中的位置次序. void output_postion(const char* a, const char* b); 如:a = ...
- 代码阅读——十个C开源项目
1. Webbench Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能,最多可以模拟3万个并发连 ...
- android即时通信开源项目信息
源码提供!Android即时通讯和sns开源项目汇总 http://www.jianshu.com/p/b2ca52337fe5
- LR、HMM、CRF和MaxEnt区别
LR:Logistic 是 Softmax 的特殊形式,多以如果 Softmax 与 MaxEnt 是等价的,则 Logistic 与 MaxEnt 是等价的. HMM模型: 将标注看作马尔可夫链,一 ...
- 07-python链接mysql
python3 中使用的是 PyMySQL模块, 取代了之前的 MysqlDB 模块, 其实使用都是一样的, 如果没有该模块的, 需要使用pip命令进行安装 pip install PyMySQL 安 ...
- DateUtils 通用类
package com.egaoqi.util; import org.apache.commons.lang3.time.DateFormatUtils; import java.text.Pars ...
- antlr提取代码注释
1. 来由 为什么要写提取注释呢,起因是工作需要.弄这么个不太重要的功能点来讲,旨在抛砖引玉. 一般而言,大家使用antlr解析源代码的时候,不会关心注释和空格之类内容,默认会过滤掉,不会放到语法树里 ...
- nginx 学习笔记(6) nginx配置文件中的度量单位
容量大小可以用比特(byte),千比特(kilobyte,后缀k或者K)或者兆(megabytes,后缀m或者M),例如:“1024”,“8k”,“1m”. 时间间隔可以用毫秒(millisecond ...
- after_create and after_commit
A relational database, like mysql, provides transactions to wrap several operations in one unit, mak ...