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     }
  • 相关阅读:
    使用Connector/C++(VS2015)连接MySQL的完整例子
    一个表里有多个字段需要同时使用字典表进行关联显示,如何写sql查询语句
    Delphi连接MySql(待测试验证,使用mysql.pas未通过)
    MySQL5.5.51启用网络远程连接
    delphi做的程序如何连接SQL数据库
    定时删除所有文件夹下的_desktop.ini文件
    Delphi中打开网页连接的几种方法
    SQL增删改查
    ADOConnection断线重连
    TDBGridEh 标头排序
  • 原文地址:https://www.cnblogs.com/davidnyc/p/8667535.html
Copyright © 2011-2022 走看看