• 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 }
     
  • 相关阅读:
    Java实现微生物增殖
    HttpClient学习整理
    在Eclipse中使用JUnit4进行单元测试(初级篇)
    http post提交数组
    postman测试post请求参数为json类型
    【springmvc】传值的几种方式&&postman接口测试
    postman的使用方法详解!最全面的教程
    Gson 使用总结 高级用法
    各个JSON技术的比较(Jackson,Gson,Fastjson)的对比
    Session保存用户名到Session域对象中
  • 原文地址:https://www.cnblogs.com/hygeia/p/4635701.html
Copyright © 2020-2023  润新知