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     }
  • 相关阅读:
    Java阻塞队列四组API介绍
    Java中常用的七个阻塞队列第二篇DelayQueue源码介绍
    Java中常用的七个阻塞队列介绍第一篇
    Java队列学习第一篇之列介绍
    Java并发之显式锁和隐式锁的区别
    网传互联网公司加班表,哈哈哈这也太真实了吧!
    Win 10 C 盘突然爆满,怎么清理?
    Java多线程并发工具类-信号量Semaphore对象讲解
    OpenStack的Neutron组件详解
    OpenStack的Cinder组件详解
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8667535.html
Copyright © 2011-2022 走看看