zoukankan      html  css  js  c++  java
  • LF.236.Search Insert Position

    Given a sorted array and a target value, return the index where it would be if it were inserted in order.

    Assumptions
    If there are multiple elements with value same as target, we should insert the target before the first existing element.

    Examples

    [1,3,5,6], 5 → 2

    [1,3,5,6], 2 → 1

    [1,3,5,6], 7 → 4

    [1,3,3,3,5,6], 3 → 1

    [1,3,5,6], 0 → 0

     1 public int searchInsert(int[] nums, int target) {
     2         //corner case
     3         if (nums == null || nums.length ==0) {
     4             return 0 ;
     5         }
     6         int left =0 , right = nums.length - 1 ;
     7         if (nums[left] > target) {
     8             return 0; // the first one
     9         } else if(nums[right] < target){
    10             return right +1 ; // the next one
    11         }
    12         //now its in the middle: if found, then return
    13         int firstIndex = 0 ;
    14         while(left + 1 < right){
    15             int mid = left + (right - left)/2 ;
    16             if (nums[mid] == target) {
    17                 right = mid ;
    18             } else if(nums[mid] < target){
    19                 left = mid ;
    20             } else {
    21                 right = mid ;
    22             }
    23         }
    24         if (nums[left] == target) {
    25             firstIndex = left ;
    26         } else if (nums[right] == target) {
    27             firstIndex = right ;
    28         } else {
    29             firstIndex = -1 ;
    30         }
    31         //the target will sit in the middle of the left and right
    32         if (firstIndex == -1 ) {
    33             return left+1;
    34         } else{
    35             return firstIndex ;
    36         }
    37     }

    note, the leetcode one does not consider the duplicate scenario:

    https://leetcode.com/problems/search-insert-position/description/

    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.

    Example 1:

    Input: [1,3,5,6], 5
    Output: 2
    

     

    Example 2:

    Input: [1,3,5,6], 2
    Output: 1
    

     

    Example 3:

    Input: [1,3,5,6], 7
    Output: 4
    

     

    Example 1:

    Input: [1,3,5,6], 0
    Output: 0



     1 public int searchInsert(int[] nums, int target) {
     2         //corner case
     3         if (nums == null || nums.length ==0) {
     4             return 0 ;
     5         }
     6         int left =0 , right = nums.length - 1 ;
     7         if (nums[left] >= target) {
     8             return 0; // the first one
     9         } else if(nums[right] == target){
    10             return right ;
    11         } else if(nums[right] < target){
    12             return right +1 ; // the next one
    13         }
    14         //now its in the middle: if found, then return
    15         while(left + 1 < right){
    16             int mid = left + (right - left)/2 ;
    17             if (nums[mid] == target) {
    18                 return mid ;
    19             } else if(nums[mid] < target){
    20                 left = mid ;
    21             } else {
    22                 right = mid ;
    23             }
    24         }
    25         // post processing is required for the left + 1 < right way
    26         if (nums[left] == target) {
    27             return left ;
    28         }
    29         if (nums[right] == target) {
    30             return right;
    31         }
    32         //the target will sit in the middle of the left and right
    33         return left+1;
    34     }
  • 相关阅读:
    windows补丁更新列表
    centos7网卡配置vlan
    exp备份工具使用说明
    windows系统SSL/TLS漏洞修复
    CDH平台:ZooKeeper 未授权访问【原理扫描】漏洞修复
    MySQL升级至5.7.35问题处理过程
    10fb does not support flow control autoneg问题处理
    vCenter异常日志:pg_tblspc找不到数据文件
    tcpdump命令
    排序算法总结
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8667535.html
Copyright © 2011-2022 走看看