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

【题目分析】

给定一个已排序的数组,数组中没有重复的数字。给定一个数字找到该数子出现的位置,如果数组中不存在该数字,则返回如果插入该数字应该插入的位置。

【思路】

涉及到已排序的数组,我们一般都会采用二分查找算法。如果目标数字存在,则直接返回结果即可。若不存在呢?

研究一下二分查找,left和right相等时,如果此时的值和目标值相同则left就是应该返回的结果,否则的话我们比较一下目标值和下标left对应的值的大小。如果目标值较大,则返回left+1,否则返回left。

【java代码】

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

LeetCode OJ 35. Search Insert Position的相关文章

[Leetcode][Python]35: Search Insert Position

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 35: Search Insert Positionhttps://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 whe

[Leetcode + Lintcode] 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 OJ:Search Insert Position(查找插入位置)

首线是自己一个比较蠢的方法,可能当时没怎么细细的想,大体的思路就是,将vector中元素存放到set中(因为set插入的时候已经排好序了),首先查找,找不到的话在插入,兵器记下插入位置,指针递增到那个地方的时候就找到了那个位置.如果第一次找到那个位置的就直接递增找到那个位置即可,代码见下,很不优雅: 1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 set<int&

【一天一道LeetCode】#35. 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,

LeetCode Problem 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]

【LeetCode OJ】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]

LeetCode练题——35. Search Insert Position

1.题目 35. Search Insert Position Easy 1781214Add to ListShare 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 th

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

本文为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