LeetCode-35

Posted 世人谓我恋长安,其实只恋长安某

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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

此题非常简单

Java代码:

public class Solution {
    public int searchInsert(int[] nums, int target) {
        int index=0;
        int i=0;
        while(i<nums.length&&nums[i]<target){
            ++i;
        }
        return i;
    }
}

  





以上是关于LeetCode-35的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 35. 搜索插入位置

leetcode 35. 搜索插入位置

LeetCode 35. Search Insert Position (Easy)

leetcode-35 search-insert-position(搜索插入位置)

Leetcode-35.搜索插入位置

算法专题(01)二分查找(02) 简单LeetCode 35