Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置

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. 若给定target小于数组第一个元素,返回0;
  2. 若target大于最后一个元素,返回n

使用二分,结束循环条件应该是high-low=1的情况。

例如[1,3,5,6]中查找2,二分一次后,low=0,high=1,此时A[low]=1,A[high]=3。

若按照平常使用的二分查找就应该找不到元素exit了,但是要返回元素的值则需要再进一步处理,此时low+1,或high-1即为元素应该的位置。

下面代码里我把target等于边界值(数组第1个和第n个)的情况放到最后比较了。

 1 class Solution {
 2 public:
 3     int searchInsert(int A[], int n, int target) {
 4         if(A == NULL || n < 1)
 5             return -1;
 6         if(target < A[0])
 7             return 0;
 8         if(target > A[n-1])
 9             return n;
10
11         int low = 0;
12         int high = n-1;
13
14         int mid = 0;
15
16         while(high-low>1){
17             mid = low + (high - low)/2;
18             if(A[mid] == target)
19                 return mid;
20             else if(A[mid] > target)
21                 high = mid;
22             else
23                 low = mid;
24         }
25
26         if(high-low ==1)
27             if(A[low] == target)
28                 return low;
29             if(A[high] == target)
30                 return high;
31             else
32                 return low+1;
33
34     }
35 };
时间: 2024-10-28 09:11:22

Search Insert Position 查找给定元素在数组中的位置,若没有则返回应该在的位置的相关文章

Search insert position, 查找插入位置

问题描述:给定一个有序序列,如果找到target,返回下标,如果找不到,返回插入位置. 算法分析:依旧利用二分查找算法. 1 public int searchInsert(int[] nums, int target) 2 { 3 return binarySearch(nums, 0, nums.length - 1, target); 4 } 5 public int binarySearch(int[] nums, int left, int right, int target) 6 {

根据给出的元素查找此元素在数组中第一次出现的索引

for循环遍历查找: public class Main{ public static void main(String[] args) { Scanner sc=new Scanner(System.in); int n=sc.nextInt(); int[] arr = {3, 78, 9, 66, 45}; int index = getIndexByEle(arr,n); System.out.println(index); } private static int getIndexBy

[LeetCode][Java] Search Insert Position

题目: 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

Leetcode 二分查找 Search Insert Position

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Search Insert Position Total Accepted: 14279 Total Submissions: 41575 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 i

【LeetCode】- Search Insert Position(查找插入的位置)

[ 问题: ] 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. 翻译:给你一个排好序的数组和一个目标值,请找出目标值可以插入数组的位置. [ 分析: ]

LeetCode 35 Search Insert Position(查找插入位置)

题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入该数字的下标 由于该数组是已经排好序的数组,可以利用二分查找. 二分查找的返回结果: 1. 当查找的数字在数组中时,返回第一次出现的下标 2. 当查找的数字不存在时,返回 - pos - 1(即 应当插入位置的相反数再减去 参考代码: package leetcode_50; import java.u

leetcode_35题——Search Insert Position(二分查找)

Search Insert Position Total Accepted: 56150 Total Submissions: 158216My Submissions Question Solution 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

LeetCode: Search Insert Position [034]

[题目] 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,

[LC]35题 Search Insert Position (搜索插入位置)

①英文题目 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], 5Output: 2Example