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

SOLUTION 1:

用九章算法的模板查找结束后判断一下即可。:

 1 public int searchInsert1(int[] A, int target) {
 2         if (A == null || A.length == 0) {
 3             return 0;
 4         }
 5
 6         int left = 0;
 7         int right = A.length - 1;
 8
 9         while (left < right - 1) {
10             int mid = left + (right - left) / 2;
11             int num = A[mid];
12
13             if (num == target) {
14                 return mid;
15             } else if (num < target) {
16                 left = mid + 1;
17             } else {
18                 right = mid - 1;
19             }
20         }
21
22         // bug 1: should use <=
23         if (target <= A[left]) {
24             return left;
25         // bug 2: should use <= . consider that may the result exit in left or right.
26         } else if (target <= A[right]) {
27             return right;
28         }
29
30         return right + 1;
31     }

SOLUTION 2:

也可以很简洁:

http://fisherlei.blogspot.com/2013/01/leetcode-search-insert-position.html

http://blog.csdn.net/fightforyourdream/article/details/14216321

这样可以word的原因是:

1. 当target存在,当然返回mid.

2. 当target大于所有的数。则l, r会跑到最右边,并且l会继续跑出去一格,也就是l会指向 len,也就是要找的值。

3. 当target小于所有的数。l,r跑到最左边,并且r会继续往左移动一格,l指向目标位置。

4. 当target小于某数a,并大于某数b。那么l, r中止时,r会在b,l 会在a,l 指向目标位置。

若是找不到target, 循环结束后l 的值是 与target最接近但是 > target 的数在数组中的位置。

 1 // sol 2:
 2     public int searchInsert(int[] A, int target) {
 3         if (A == null || A.length == 0) {
 4             return 0;
 5         }
 6
 7         int left = 0;
 8         int right = A.length - 1;
 9
10         while (left <= right) {
11             int mid = left + (right - left) / 2;
12             int num = A[mid];
13
14             if (num == target) {
15                 return mid;
16             } else if (num < target) {
17                 left = mid + 1;
18             } else {
19                 right = mid - 1;
20             }
21         }
22
23         return left;
24     }

GTIHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/divide2/SearchInsert.java

时间: 2024-08-04 18:22:45

LeetCode: Search Insert Position 解题报告的相关文章

[LeetCode] Search Insert Position [21]

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

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

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,

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

Leetcode35 Search Insert Position 解题思路(python)

本人编程小白,如果有写的不对.或者能更完善的地方请个位批评指正! 这个是leetcode的第35题,这道题的tag是数组,python里面叫list,需要用到二分搜索法 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 in

[leetcode]Search Insert Position @ Python

原题地址:https://oj.leetcode.com/problems/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

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. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

[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. Here are few examples.[1,3,5,6], 5 → 2[1,3,5,6], 2 →

LeetCode Search Insert Position (二分查找)

题意: 给一个升序的数组,如果target在里面存在了,返回其下标,若不存在,返回其插入后的下标. 思路: 来一个简单的二分查找就行了,注意边界. 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums,int target) 4 { 5 int L=0, R=nums.size(); 6 while(L<R) 7 { 8 int mid=R-(R-L+1)/2; 9 if(nums[mid]>=t