LeetCode 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.

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



题目标签:Array

  这道题目给了我们一个有序序列,让我们找到target的位置,如果没有target,找到有序嵌入target的位置。利用二分法,来找target,如果没有target,那么最后返回left,就是我们需要嵌入的位置。

  我们来根据code 看原题的例子,当mid < target的话,left 等于 mid + 1; while 条件是包括 left = right。

  [1,3,5,6], 2 -> mid = 3,3 大于2的, 那么范围缩小到 1这个数字, left 和 right 都指向1。1是大于2的,那么left = mid + 1, left 就指向了3, 并且while loop 结束了。 3这个位置就是需要嵌入的地方。

  [1,3,5,6], 7 -> mid = 3, 3 小于 7 的, 那么范围缩小到 left 指向5, right 指向6。 mid = 5, 依然小于7, 那么 left = mid + 1,  left 指向6, right 也指向6。mid = 6, 再一次小于7, left = mid + 1, 就超出了范围,但是这个时候left 指向的是6 后面一位,就是嵌入的位置。

  [1,3,5,6], 0 -> mid = 3 大于0, right = mid - 1; left 指向1, right 指向1。mid = 1, 依然大于0, right = mid - 1;这时候right 已经等于 -1 了。超出了范围,但是left 依然是 0, 而这个 index = 0 的位置就是需要嵌入的位置。

  根据这三个例子的特性,要嵌入的位置,在array 左边是不能超出的,右边是可以超出的。所以我们只要最后return left 就可以了。因为left 只会超过右边。

Java Solution:

Runtime beats 70.88%

完成日期:03/28/2017

关键词:Array

关键点:二分法

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

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

时间: 2024-12-26 20:52:55

LeetCode 35. Search Insert Position (搜索嵌入的位置)的相关文章

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

LeetCode 35. Search Insert Position (Easy)

上班无聊就在leetCode刷刷题目,有点遗憾的是到现在才去刷算法题,大学那会好好利用该多好啊. 第35道简单算法题,一次性通过有点开心,分享自己的代码. 问题描述 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 assu

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

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 二分查找(二分下标)

基础题之一,是混迹于各种难题的基础,有时会在小公司的大题见到,但更多的是见于选择题... 题意:在一个有序数列中,要插入数target,找出插入的位置. 楼主在这里更新了<二分查找综述>第一题的解法,比较类似,当然是今天临时写的. 知道了这题就完成了leetcode 4的第二重二分的写法了吧,楼主懒... 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 in

LeetCode 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 ma

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

LeetCode 35 Search Insert Position (C,C++,Java,Python)

Problem: 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,