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. 后端对数组json_encode,前端遍历输出

    echo json_encode($get_city_lists); <script type="text/javascript"> function get_city ...

  2. SQL手动注入解析

    作者:震灵 注入环境:DVWA 探测步骤: 1.首先探测是否可以注入以及注入方式 原SQL语句为 SELECT * FROM xxx WHERE a=''; 注入后为 SELECT * FROM xx ...

  3. CentOS6.4虚拟机设置固定IP、安装JDK、Tomcat、Redis并部署web项目

    一.CentOS设置固定IP 1.直接修改配置文件的方式,原文地址:http://www.cnblogs.com/zhja/p/3964159.html (1)首先获取你的GATEWAY 方便后面在c ...

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

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

  5. dashDB - Introduction and DB Tools

    dashDB - Introduction dashDB is a database that is designed for performance and scale. It offers sea ...

  6. Vue.js 基本功能了解

    一.写在前面 隔了这么久才来出Vue的第二篇文章,真是堕落了,自己先惩罚下/(ㄒoㄒ)/~~ 回过头看自己第一篇相关文章<初试 Vue.js>(http://www.cnblogs.com ...

  7. chrome浏览器iframe兼容性问题,隐藏起来再显示滚动条消失?

    前言:在调试页面时发现谷歌浏览器bug,版本: 58.0.3029.81 问题描述: 1. 页面中,选项卡里面是IFrame,页面初始显示时有纵向滚动条出现 2. 来回切换选项卡一次,原来选项卡页面的 ...

  8. visual studio问题集合

    1.当前断点不会命中,还没有为该文档加载任何符号 打开visual 2010 工具->选项->调试->符号: 一.手动加载 点击 加载所有符号 即可.二.自动加载 点击"指 ...

  9. jvm学习002 虚拟机类加载过程以及主动引用和被动引用

    虚拟机把描述类的数据从class文件加载到内存,并对数据进行校验,转换解析和初始化,最终形成可以被虚拟机直接使用的Java类型,这就是虚拟机的类加载机制. 类从被加载到虚拟机内存中开始,到卸载出内存为 ...

  10. 用caffe一步一步实现人脸检测

    学习深度学习已有一段时间了,总想着拿它做点什么,今天终于完成了一个基于caffe的人脸检测,这篇博文将告诉你怎样通过caffe一步步实现人脸检测.本文主要参考唐宇迪老师的教程,在这里感谢老师的辛勤付出 ...