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

 public class Solution {
public int searchInsert(int[] nums, int target) {
int len=nums.length;
for(int i =0;i<len;i++){
if(nums[i]==target){
return i;
}
else if(nums[i]<target&&i+1<len&&nums[i+1]>target){
return i+1;
}
else if(nums[len-1]<target){
return len; }
else if(nums[0]>target){
return 0;
}
}
return 0;
}
}

解题思路:

  1. 判断边界值最小返回0最大返回数组长度,和某个值一样返回该值下标+1,在两个元素之间返回较大元素下标
  2. 注意在两个元素之间的时候判断防止溢出
  3. // version 1: find the first position >= target
    public class Solution {
    public int searchInsert(int[] A, int target) {
    if (A == null || A.length == 0) {
    return 0;
    }
    int start = 0, end = A.length - 1; while (start + 1 < end) {
    int mid = start + (end - start) / 2;
    if (A[mid] == target) {
    return mid;
    } else if (A[mid] < target) {
    start = mid;
    } else {
    end = mid;
    }
    } if (A[start] >= target) {
    return start;
    } else if (A[end] >= target) {
    return end;
    } else {
    return end + 1;
    }
    }
    } // version 2: find the last position < target, return +1, 要特判一下target小于所有数组里面的元素 public class Solution {
    public int searchInsert(int[] A, int target) {
    if (A == null || A.length == 0) {
    return 0;
    }
    int start = 0;
    int end = A.length - 1;
    int mid; if (target < A[0]) {
    return 0;
    }
    // find the last number less than target
    while (start + 1 < end) {
    mid = start + (end - start) / 2;
    if (A[mid] == target) {
    return mid;
    } else if (A[mid] < target) {
    start = mid;
    } else {
    end = mid;
    }
    } if (A[end] == target) {
    return end;
    }
    if (A[end] < target) {
    return end + 1;
    }
    if (A[start] == target) {
    return start;
    }
    return start + 1;
    }
    }
  4. 以上两个代码为两种更高效的方法

35. Search Insert Position【leetcode】的更多相关文章

  1. 60. Search Insert Position 【easy】

    60. Search Insert Position [easy] Given a sorted array and a target value, return the index if the t ...

  2. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  3. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  4. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

  5. LeetCode练题——35. Search Insert Position

    1.题目 35. Search Insert Position Easy 1781214Add to ListShare Given a sorted array and a target value ...

  6. 【LeetCode】35. Search Insert Position (2 solutions)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  7. 35. Search Insert Position@python

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

  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 OJ 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. 拥抱.NET Core系列:依赖注入(1)

    依赖注入时编程手段中解耦和封装的一个非常重要的手段,我本人已经到了没有DI无法编写项目的程度了,在.NET Framework中微软并没有在FCL中引入DI,虽然推出了"Unity" ...

  2. CSS3学习系列之布局样式(二)

    使用盒布局 在CSS3中,通过box属性来使用盒布局,例子如下: <!DOCTYPE html> <html lang="en"> <head> ...

  3. FileMethods

    }else { System.out.println("不能写入此文件"); } System.out.println("此文件最后修改时间是2000年1月1日后的&qu ...

  4. 51Nod 1509加长棒

    传送门 http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1509 现在有三根木棒,他们的长度分别是a,b,c厘米.你可以对他 ...

  5. centos7使用cobbler(2.8)批量部署操作系统之一

    一.    批量部署操作系统的前提 要想批量部署操作系统,得具备以下条件: 客户机支持pxe网络引导 服务器端和客户端建立网络通信(DHCP) 服务器端要有可供客户机开机引导的引导文件 服务器端的可引 ...

  6. ArrayList源码浅析(jdk1.8)

    ArrayList的实质就是动态数组.所以可以通过下标准确的找到目标元素,因此查找的效率高.但是添加或删除元素会涉及到大量元素的位置移动,所以效率低. 一.构造方法 ArrayList提供了3个构造方 ...

  7. 全网首创ISE入门级教程

    转眼间我已经大三了,现在成为了实验室的负责人,对于下一届学生的纳新重任就交到了我的手上,想采取不同的方法暑假尽可能对他们进行一些培训,所以制作了此教程,说实话,在网上还没有找到关于ISE的入门级使用教 ...

  8. KMP算法(研究总结,字符串)

    KMP算法(研究总结,字符串) 前段时间学习KMP算法,感觉有些复杂,不过好歹是弄懂啦,简单地记录一下,方便以后自己回忆. 引入 首先我们来看一个例子,现在有两个字符串A和B,问你在A中是否有B,有几 ...

  9. Java之面向对象例子(二)

    定义一个Book类,在定义一个JavaBook类继承他 //book类 package com.hanqi.maya.model; public class Book { public String ...

  10. linux DNS 问题

    今天准备爬虫51job时候,发现ping不通外网了,ping 了一下IP,都是OK的,只是host不通. 呵呵,就一DNS问题,好的.第一步,开始检查配置文件 cat /etc/sysconfig/n ...