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

这道题基本没有什么难度,实在不理解为啥还是Medium难度的,完完全全的应该是Easy啊,三行代码搞定的题,只需要遍历一遍原数组,若当前数字大于或等于目标值,则返回当前坐标,如果遍历结束了,说明目标值比数组中任何一个数都要大,则返回数组长度n即可,代码如下:

解法一:

class Solution {
public:int searchInsert(vector<int>& nums, int target) {for (int i = 0; i < nums.size(); ++i) {if (nums[i] >= target) return i;}return nums.size();}
};

当然,我们还可以用二分搜索法来优化我们的时间复杂度,而且个人认为这种方法应该是面试官们想要考察的算法吧,参见代码如下:

解法二:

class Solution {
public:int searchInsert(vector<int>& nums, int target) {if (nums.back() < target) return nums.size();int left = 0, right = nums.size() - 1;while (left < right) {int mid = left + (right - left) / 2;if (nums[mid] == target) return mid;else if (nums[mid] < target) left = mid + 1;else right = mid;}return right;}
};

LeetCode All in One 题目讲解汇总(持续更新中...)

转载于:https://www.cnblogs.com/grandyang/p/4408638.html

[LeetCode] Search Insert Position 搜索插入位置相关推荐

  1. leetcode -- Search Insert Position

    2019独角兽企业重金招聘Python工程师标准>>> Search Insert Position Given a sorted array and a target value, ...

  2. LeetCode --Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  3. [LeetCode]Search Insert Position

    原题链接:http://oj.leetcode.com/problems/search-insert-position/ 题意描述: Given a sorted array and a target ...

  4. Leetcode:Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  5. LeetCode Search Insert Position (二分查找)

    题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. 1 class Solution { 2 publi ...

  6. LeetCode算法入门- Search Insert Position -day19

    LeetCode算法入门- Search Insert Position -day19 题目描述 Given a sorted array and a target value, return the ...

  7. 【二分法】LeetCode 35. Search Insert Position

    LeetCode 35. Search Insert Position Solution1:我的答案 class Solution { public:int searchInsert(vector&l ...

  8. LeetCode - 35. Search Insert Position

    35. Search Insert Position Problem's Link ---------------------------------------------------------- ...

  9. LeetCode题解:35.搜索插入位置

    搜索插入位置(easy) 更好的阅读体验应该是: 审题-思考 答题 整理-归纳 一.题目 LeetCode题目链接:35.搜索插入的位置 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引 ...

最新文章

  1. Linux进程间通信二 System V 消息队列简介与示例
  2. 关于MonoDevelop自动缩进的设置
  3. python+unittest框架整理(一点点学习前辈们的封装思路,一点点成长。。。)
  4. python tkinter进度条_在python tkinter中Canvas实现进度条显示的方法
  5. 运行github上的Vue项目
  6. 记忆碎片---搭建php+apache+eclipse中的问题
  7. 甲骨文每季安全更新再修补297个漏洞
  8. 可预见的数字化未来:在雄安再造爱沙尼亚
  9. 0ctf-2016 pwn-warmup writeup
  10. Js学习之拖拉事件(drag)
  11. Shell修改命令提示符
  12. Gitee 自已提交的代码提交人头像却为他人
  13. 死囚试毒酒问题(改编)
  14. Blah数集(信息学奥赛一本通 - T1333)
  15. 免备案网站搭建,香港服务器
  16. 嵌入式系统项目设计——电子琴(完整代码)
  17. 图像处理进阶——去雾算法
  18. 网络身份安全中的数据策略问题
  19. 惠普z240工作站装Linux,已解决: 求解如何加装硬件提高z240sff工作站性能 - 惠普支持社区 - 820413...
  20. 高校动态|北大“韦神”获达摩院青橙奖,奖金100万元!一同获奖的还有他们!

热门文章

  1. 多层感知器(MLP)详解【基于印第安人糖尿病数据】
  2. Python程序设计题解【蓝桥杯官网题库】 DAY15-算法训练
  3. Java的基础方法Java的对象_java基础之 创建对象的几种方式
  4. usb大容量存储设备驱动_Win10默认已禁用USB驱动器缓存 1903版本起无需点击安全弹出...
  5. eslint不报错 vue_【简易教程】基于Vue-cli使用eslint指南
  6. android检查usb广播,Android 检测USB 音频设备
  7. mysql error manager,MYSQL Starting MySQL. ERROR! Manager of pid-file quit without updating file
  8. php 文档在线查看器,Office Web Viewer 在线Office文档查看器API
  9. python如何启动前端_python- 前端进阶
  10. python123数字形式转换_【Python系统学习02】数据类型与类型转换