zoukankan      html  css  js  c++  java
  • 35. Search Insert Position【leetcode】

    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 public class Solution {
     2     public int searchInsert(int[] nums, int target) {
     3         int len=nums.length;
     4         for(int i =0;i<len;i++){
     5             if(nums[i]==target){
     6                 return i;
     7             }
     8             else if(nums[i]<target&&i+1<len&&nums[i+1]>target){
     9                     return i+1;
    10                 }
    11             else if(nums[len-1]<target){
    12                 return len;
    13             
    14             }
    15             else if(nums[0]>target){
    16                 return 0;
    17             }
    18         }
    19         return 0;
    20     }
    21 }

    解题思路:

    1. 判断边界值最小返回0最大返回数组长度,和某个值一样返回该值下标+1,在两个元素之间返回较大元素下标
    2. 注意在两个元素之间的时候判断防止溢出
    3. // version 1: find the first position >= target
      public class Solution {
          public int searchInsert(int[] A, int target) {
              if (A == null || A.length == 0) {
                  return 0;
              }
              int start = 0, end = A.length - 1;
              
              while (start + 1 < end) {
                  int mid = start + (end - start) / 2;
                  if (A[mid] == target) {
                      return mid;
                  } else if (A[mid] < target) {
                      start = mid;
                  } else {
                      end = mid;
                  }
              }
              
              if (A[start] >= target) {
                  return start;
              } else if (A[end] >= target) {
                  return end;
              } else {
                  return end + 1;
              }
          }
      }
      
      // version 2: find the last position < target, return +1, 要特判一下target小于所有数组里面的元素
      
      public class Solution {
          public int searchInsert(int[] A, int target) {
              if (A == null || A.length == 0) {
                  return 0;
              }
              int start = 0;
              int end = A.length - 1;
              int mid;
              
              if (target < A[0]) {
                  return 0;
              }
              // find the last number less than target
              while (start + 1 < end) {
                  mid = start + (end - start) / 2;
                  if (A[mid] == target) {
                      return mid;
                  } else if (A[mid] < target) {
                      start = mid;
                  } else {
                      end = mid;
                  }
              }
              
              if (A[end] == target) {
                  return end;
              }
              if (A[end] < target) {
                  return end + 1;
              }
              if (A[start] == target) {
                  return start;
              }
              return start + 1;
          }
      }
    4. 以上两个代码为两种更高效的方法
    不积跬步无以至千里,千里之堤毁于蚁穴。 你是点滴积累成就你,你的丝丝懒惰毁掉你。 与诸君共勉
  • 相关阅读:
    Redis学习笔记(十四)Sentinel(哨兵)(上)
    Redis学习笔记(十三) 复制(下)
    Redis学习笔记(十二) 复制(上)
    搞懂推荐系统中的评价指标NDCG(CG、DCG、IDCG)
    pytorch入门3.2构建分类模型再体验(批处理)
    pytorch入门3.1构建分类模型再体验(模型和训练)
    pytorch入门3.0构建分类模型再体验(准备数据)
    一条一条的读《推荐系统实战》2.0
    pytorch入门2.2构建回归模型初体验(开始训练)
    pytorch入门2.1构建回归模型初体验(模型构建)
  • 原文地址:https://www.cnblogs.com/haoHaoStudyShare/p/7337069.html
Copyright © 2011-2022 走看看