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     }
  • 相关阅读:
    二.线性表
    一.绪论
    托管服务器代码
    jquery easyui DataGrid 数据表格 属性
    用socket 模拟http请求
    struct和class的相同点与不同点
    c++中的基本数据类型
    当函数返回一个局部变量的指针问题,内存是否被释放
    Python首先生成包含1000个随机字符的字符串,然后统计每个字符的出现次数。(使用字典)
    浅谈结构体字节的求法
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8667535.html
Copyright © 2011-2022 走看看