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

public class Solution {
    public int searchInsert(int[] nums, int target) {
        //二分法的变形,没有找到时,返回的是应该插入的下标
        return find(nums,target,0,nums.length-1);

    }
    public int find(int []nums,int target,int start,int end){
        if(start>end) return start;//二分法唯一的不同
        int mid=(start+end)/2;
        if(nums[mid]==target) return mid;
        if(nums[mid]>target){
            return find(nums,target,start,mid-1);
        }else{
            return find(nums,target,mid+1,end);
        }

    }
}
时间: 2024-11-05 14:52:03

[leedcode 35] Search Insert Position的相关文章

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

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,6], 5 → 2[1,3,5,6], 2 →

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

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

35. Search Insert Position(C++)

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 →