zoukankan      html  css  js  c++  java
  • Search Insert Position

    题目

    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

    解题思路:

     1 /**
     2  * 本代码由九章算法编辑提供。没有版权欢迎转发。
     3  * - 九章算法致力于帮助更多中国人找到好的工作,教师团队均来自硅谷和国内的一线大公司在职工程师。
     4  * - 现有的面试培训课程包括:九章算法班,系统设计班,BAT国内班
     5  * - 更多详情请见官方网站:http://www.jiuzhang.com/
     6  */
     7 
     8 // version 1: find the first position >= target
     9 public class Solution {
    10     public int searchInsert(int[] A, int target) {
    11         if (A == null || A.length == 0) {
    12             return 0;
    13         }
    14         int start = 0, end = A.length - 1;
    15         
    16         while (start + 1 < end) {
    17             int mid = start + (end - start) / 2;
    18             if (A[mid] == target) {
    19                 return mid;
    20             } else if (A[mid] < target) {
    21                 start = mid;
    22             } else {
    23                 end = mid;
    24             }
    25         }
    26         
    27         if (A[start] >= target) {
    28             return start;
    29         } else if (A[end] >= target) {
    30             return end;
    31         } else {
    32             return end + 1;
    33         }
    34     }
    35 }
    36 
    37 // version 2: find the last position < target, return +1
    38 
    39 public class Solution {
    40     public int searchInsert(int[] A, int target) {
    41         if (A == null || A.length == 0) {
    42             return 0;
    43         }
    44         int start = 0;
    45         int end = A.length - 1;
    46         int mid;
    47         
    48         if (target < A[0]) {
    49             return 0;
    50         }
    51         // find the last number less than target
    52         while (start + 1 < end) {
    53             mid = start + (end - start) / 2;
    54             if (A[mid] == target) {
    55                 return mid;
    56             } else if (A[mid] < target) {
    57                 start = mid;
    58             } else {
    59                 end = mid;
    60             }
    61         }
    62         
    63         if (A[end] == target) {
    64             return end;
    65         }
    66         if (A[end] < target) {
    67             return end + 1;
    68         }
    69         if (A[start] == target) {
    70             return start;
    71         }
    72         return start + 1;
    73     }
    74 }
     
  • 相关阅读:
    js对象的所有方法
    js数组的所有方法
    Scss语法
    new一个对象的过程
    promises的深入学习
    jsonp的原理介绍及Promise封装
    Vue页面缓存和不缓存的方法
    JavaScript数据类型
    JS常用函数原理的实现
    @Autowired注解在抽象类中实效的原因分析
  • 原文地址:https://www.cnblogs.com/hygeia/p/4635701.html
Copyright © 2011-2022 走看看