【leetcode】Search Insert Position

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

采用[0,n-1]左闭右闭的方式查找

考虑几种情况

[1,2]查找0

第一步:left=0,right=1,A[mid]=1>0  ===> right=1-1=0

第二步:left=0,right=0,A[mid]=1>0 ===> right=0-1=-1

停止,返回left即可

[1,2]查找3

第一步:left=0,right=1,A[mid]=1<3 ===>left=0+1=1

第二部:left=1,right=1,A[mid]=2<3 ===>left=1+1=2

停止,返回left即可

[1,2]查找1.5

第一步:left=0,right=1,A[mid]=1<1.5 ===>left=0+1=1

第二步:left=1,right=1,A[mid]=2>1.5 ===>right=1-1=0

停止,返回left即可

综上,如果查找不到,只需要返回left即可

采用二分查找,关于二分查找

 1 class Solution {
 2 public:
 3     int searchInsert(int A[], int n, int target) {
 4
 5         int left=0;
 6         int right=n-1;
 7         int mid;
 8         while(left<=right)
 9         {
10             mid=(left+right)/2;
11
12             if(A[mid]<target)
13             {
14                 left=mid+1;
15             }
16             else if(A[mid]>target)
17             {
18                 right=mid-1;
19             }
20             else
21             {
22                 return mid;
23             }
24         }
25
26         return left;
27     }
28 };
时间: 2024-12-20 16:06:50

【leetcode】Search Insert Position的相关文章

【LeetCode】Search Insert Position (2 solutions)

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,

【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] 035. Search Insert Position (Medium) (C++)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 035. Search Insert Position (Medium) 链接: 题目:https://leetcode.com/problems/search-insert-position/ 代码(github):https://github.com/illuz/leetcode 题意: 要把一个数有序插入到一

【Leetcode】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

LeetCode 035 Search Insert Position

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

leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法

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

【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】Search in Rotated Sorted Array 解题报告

[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise return -1. You may assume no d

【LeetCode】Search in Rotated Sorted Array II 解题报告

[题目] Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array. [解析] 相比Search in Rotated Sorted Array,在