Search Insert Position 解答

Question

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 -- Naive

Iterate the array and compare target with ith and (i+1)th element. Time complexity O(n).

 1 public class Solution {
 2     public int searchInsert(int[] nums, int target) {
 3         if(nums==null) return 0;
 4         if(target <= nums[0]) return 0;
 5         for(int i=0; i<nums.length-1; i++){
 6             if(target > nums[i] && target <= nums[i+1]){
 7                 return i+1;
 8             }
 9         }
10         return nums.length;
11     }
12 }

Solution 2 -- Binary Search

If the target number doesn‘t exist in original array, then after iteration, it must be pointed by low pointer.

Time complexity O(log(n))

 1 public class Solution {
 2     public int searchInsert(int[] nums, int target) {
 3         if (nums == null || nums.length == 0)
 4             return 0;
 5         int start = 0, end = nums.length - 1, mid = (end - start) / 2 + start;
 6         while (start <= end) {
 7             mid = (end - start) / 2 + start;
 8             if (nums[mid] == target)
 9                 return mid;
10             else if (nums[mid] < target)
11                 start = mid + 1;
12             else
13                 end = mid - 1;
14         }
15         return start;
16     }
17 }
时间: 2024-08-08 22:04:43

Search Insert Position 解答的相关文章

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

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 解题报告

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,

Search Insert Position (LeetCode)

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 (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] 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 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 二分查找 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