题目描述

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

解析:该题目一看就像是插入排序的思路。不过我们可以通过观察测试用例来做。分4种情况讨论:
1.target<=最小值;
2.target==最大值;
3.target>最大值;
4.target介于最小值与最大值之间
需要分两种情况
(1)target==当前值,直接返回当前值的下标;
(2)target>当前值,小于下一个值,就返回下一个值的下标

public class Solution {public int searchInsert(int[] A, int target) {if(target<=A[0]){//1.target<=最小值;return 0;}else if(target==A[A.length-1]){//2.target==最大值;return A.length-1;}else if(target>A[A.length-1]){//3.target>最大值;return A.length;}else{//target介于最小值与最大值之间for(int i=0;i<A.length-1;i++){if(A[i]==target){//(1)target==当前值,直接返回当前值的下标;return i;}if(target>A[i]&&target<A[i+1]){//(2)target>当前值,小于下一个值,就返回下一个值的下标return i+1;}}}return -1;}
}

《leetcode》search-insert-position相关推荐

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

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

  2. LeetCode - 35. Search Insert Position

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

  3. leetcode 【 Search Insert Position 】python 实现

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

  4. LeetCode T35 Search Insert Position

    文章目录 题目地址 题目描述 思路 题解 题目地址 中文:https://leetcode-cn.com/problems/search-insert-position/ 英文:https://lee ...

  5. 【LeetCode】- Search Insert Position(查找插入的位置)

    [ 问题: ] Given a sorted array and a target value, return the index if the target is found. If not, re ...

  6. leetcode 35 Search Insert Position(二分法)

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

  7. LeetCode之Search Insert Position

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

  8. [LeetCode] 35. Search Insert Position

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

  9. LeetCode 35. Search Insert Position

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

  10. [LeetCode]--35. Search Insert Position

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

最新文章

  1. java设计模式-适配器模式
  2. Python 统计列表中元素出现的次数
  3. 近世代数--有限交换群--存在元素的阶是群阶的素因子
  4. android开发关于和使用本机内存,内置存储卡和外置存储卡大揭秘
  5. 干燥剂变身“配重剂”,究竟有多少商家在休闲零食市场浑水摸鱼?
  6. 职业高中高一计算机的基本知识,职业高中计算机论文
  7. c#调用javascript的方法,有Updatepanel的情况
  8. RMQ with Shifts
  9. 转 Linux调优方案,sysctl.conf的设置
  10. 04 _ 可扩展架构案例(一):电商平台架构是如何演变的?
  11. BPSK调制解调的matlab设计和FPGA实现之matlab设计
  12. Linux安装python第三方库的步骤
  13. 3D全息投影 大家可以自己动手制作
  14. Mac恢复出厂模式教程(Intel 版)
  15. Lambda拉姆达表达式使用学习
  16. (零基础)如何使用python下载哔哩哔哩视频?
  17. 基于C语言设计的小型图形软件系统
  18. 策略模式——商品促销
  19. pf与ckf_基于CKF-PF算法在高速动车组定位中的应用研究
  20. MBA联考逻辑真题1

热门文章

  1. 简单易懂,ThreadPoolExecutor参数说明
  2. 笛卡尔树 (25 分)笛卡尔树是一种特殊的二叉树,其结点包含两个关键字K1和K2。首先笛卡尔树是关于K1的二叉搜索树,即结点左子树的所有K1值都比该结点的K1值小,右子树则大。其次所有结点的K2关键字
  3. java转文件编码bom_编码转换:UTF-8 BOM to GBK
  4. Nginx+Tomcat负载均衡、动静分离集群
  5. java 如何将word 转换为ftl_如何把word转化为pdf?职场达人必需的转换技巧
  6. (Spinner) android中Spinner的使用
  7. mysql组合索最左_MySQL组合索引和最左匹配原则
  8. android 显示字符串最后一个字,android – 在textview中显示的最后一个单词
  9. 批量梯度下降 linux,梯度下降的三种形式BGD,SGD,MBGD(MSGD)
  10. 三层交换机能传递路由吗?_华为ensp三层交换机VLAN配置静态路由互通