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     {
 7         int mid = (left + right)/2;
 8         if(left > right)
 9         {
10             return left;
11         }
12         if(nums[mid] == target)
13         {
14             return mid;
15         }
16         else if(nums[mid] < target)
17         {
18             return binarySearch(nums, mid + 1, right, target);
19         }
20         else
21         {
22             return binarySearch(nums, left, mid - 1, target);
23         }
24     }
时间: 2024-10-27 12:09:12

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] 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 →

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

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

[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

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 [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]