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. Chapter 4. The MPEG-4 and H.264 Standards

    本章节介绍一些关于MPEG-4标准与H.264标准的基本知识 比较重要的是第95页关于两种标准的对比表格.其他部分没有什么特别重要的细节.

  2. php文件的管理

    一.先做一下简单的查看文件功能,文件中的文件和文件夹都显示,但是双击文件夹可以显示下一级子目录,双击"返回上一级"就可以返回到上一级目录 (1)先将需要管理的文件遍历出来,可以加个 ...

  3. tokuDB 安装与备份小记

    线上的数据增长越来越快,数据量的增长也愈来愈大,尤其是日志类数据,这对于数据迁移.数据备份恢复而言,都是一个非常大的挑战. 于是想到了 13 年开源 tokuDB 存储引擎,来解决我们迫在眉睫的容量问 ...

  4. 【原创】Kafka 0.11消息设计

    Kafka 0.11版本增加了很多新功能,包括支持事务.精确一次处理语义和幂等producer等,而实现这些新功能的前提就是要提供支持这些功能的新版本消息格式,同时也要维护与老版本的兼容性.本文将详细 ...

  5. H3CNE实验:配置VLAN和VLAN端口

    配置准备数据: | 设备名称 | IP地址 | VLAN网关 | 接口 | VLAN | |---------------|--------------|----------------|------ ...

  6. Java之初识

    今天开始学习Java 1.什么是Java? Java是1995年由sun公司推出的一门极富创造力的面向对象编程语言,是由Java之父詹姆斯格斯林博士设计的. Java名字的由来:据说,java刚刚设计 ...

  7. AngularJS 和 Bootstrap

    AngularJS Bootstrap AngularJS 的首选样式表是 Twitter Bootstrap, Twitter Bootstrap 是目前最受欢迎的前端框架. 查看 Bootstra ...

  8. Python爬虫从入门到放弃(十一)之 Scrapy框架整体的一个了解

    这里是通过爬取伯乐在线的全部文章为例子,让自己先对scrapy进行一个整理的理解 该例子中的详细代码会放到我的github地址:https://github.com/pythonsite/spider ...

  9. Invert a binary tree 翻转一棵二叉树

    Invert a binary tree 翻转一棵二叉树 假设有如下一棵二叉树: 4  / \   2    7  / \   / \ 1  3 6  9翻转后: 4     /    \    7 ...

  10. jenkins外网slave配置

    背景: 客户提供了测试服务器,但不能外网直连需要通过windows跳板进行进行连接. 方案设定将windows跳板机配置为远程slave节点. 技术支持: jenkins-slave  windows ...