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. Hibernate缓存和懒加载的坑你知道多少?这5个简单问题回答不上来就不敢说会用hibernate

    问题1:session.flush()调用之后,懒加载还生效吗? 如果不生效,那是抛异常还是没有任何反应,或者直接返回null? 答案:生效.可以理解为在同一个session当中,懒加载只会执行一次. ...

  2. 【LeetCode】289. Game of Life

    题目: According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a ce ...

  3. ajax数据请求3(数组json格式)

    ajax数据请求3(数组json格式) <!doctype html> <html> <head> <meta charset="utf-8&quo ...

  4. 微信支付(APP)

    折腾了一天,终于搞定了微信支付,总结一下.首先从服务器端获取prepareid,Andorid 端再根据这个prepareid二次签名. 服务器端: 从官网上下载DEMO,Demo中只有JsAPi,M ...

  5. MAC 相关操作解析

    MAC 相关操作解析 OS 显示桌面 f11 F1~F12 fn + F1~F12 撤销重做 command + z command + shift + z 图片预览 选择图片 空格 上下左右 svn ...

  6. 妙用Outlook2003群发商业邮件

    妙用Outlook2003群发商业邮件   我们知道,如果需要在Outlook 2003中向多个对象发送邮件,那么只需要在指定收件人时用分号输入多个邮件地址或者使用抄送方式即可:假如对象较多,可以使用 ...

  7. Asp.Net MVC-01-起步

    创建第一个MVC程序 我们先创建一个ASP.NET Web程序 模板选择MVC,因为不想使用默认的身份认证我们点击更改身份认证并选择不进行身份认证. 创建的项目结构如下: 配置与初始化 Web配置文件 ...

  8. vue实现对表格数据的增删改查

    在管理员的一些后台页面里,个人中心里的数据列表里,都会有对这些数据进行增删改查的操作.比如在管理员后台的用户列表里,我们可以录入新用户的信息,也可以对既有的用户信息进行修改.在vue中,我们更应该专注 ...

  9. springmvc(一) springmvc框架原理分析和简单入门程序

    springmvc这个框架真的非常简单,感觉比struts2还更简单,好好沉淀下来学习~ --WH 一.什么是springmvc? 我们知道三层架构的思想,并且如果你知道ssh的话,就会更加透彻的理解 ...

  10. Java读取property配置文件

    读取配置文件已经成了Java程序员工作的一项必备技能. 配置文件的优点: 可维护性好 怎么个可维护性好呢? 它会让程序中变化的地方很灵活的配置,不需要修改代码. Java程序部署到服务器上去之后就变成 ...