35. 搜索插入位置

35. 搜索插入位置

https://leetcode-cn.com/problems/search-insert-position/description/

package com.test;

public class Lesson035 {
    public static void main(String[] args) {
        int[] nums = {1,3,5,6};
        int target = 5;
        int in = searchInsert(nums, target);
        System.out.println(in);
    }
    public static int searchInsert(int[] nums, int target) {
        for(int i=0;i<nums.length;i++) {
            // 如果相等就找到了
            if (target - nums[i]==0) {
                return i;
            }
            // 如果比第i个元素小
            if (target < nums[i]) {
                // 如果比第一个元素小
                if (i == 0) {
                    return 0;
                }
                // 如果比第i个元素小,并且比第i-1个元素大
                if (i > 0 && target > nums[i - 1]) {
                    return i;
                }
            }
            // 如果比第i个元素大
            if (target > nums[i]) {
                // 如果比最后一个元素大
                if (i - nums.length == -1) {
                    return nums.length;
                }
                // 如果比第i个元素大,并且比第i+1个元素小
                if (i + 1 < nums.length && target < nums[i + 1]) {
                    return i+1;
                }
            }
        }
        return 0;
    }
}

二分查找:

原文地址:https://www.cnblogs.com/stono/p/9501188.html

时间: 2024-08-04 12:30:23

35. 搜索插入位置的相关文章

[leetcode] 35. 搜索插入位置(Java)(二分)

35. 搜索插入位置 二分,太简单,没啥好说的 class Solution { public int searchInsert(int[] nums, int target) { if (nums.length == 0) return 0; int i = 0, j = nums.length; int mid = (i + j) / 2; while (i < j) { if (nums[mid] == target) { return mid; } else if (nums[mid]

LeetCode刷题--35. 搜索插入位置(简单)

题目描述 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5输出: 2 示例 2: 输入: [1,3,5,6], 2输出: 1 示例 3: 输入: [1,3,5,6], 7输出: 4 示例 4: 输入: [1,3,5,6], 0输出: 0 思路:二分法 如果该题目暴力解决的话需要 O(n)的时间复杂度,但是如果二分的话则可以降低到 O(logn)的时间

Leetcode 35.搜索插入位置 By Python

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输出: 2 示例 2: 输入: [1,3,5,6], 2 输出: 1 示例 3: 输入: [1,3,5,6], 7 输出: 4 示例 4: 输入: [1,3,5,6], 0 输出: 0 思路 python的list是有index()方法的,如果目标值在数组中就很简单 如果没有的话就从头线性扫描一遍,当

leetCode 第35题,搜索插入位置

题目概述 题目:力扣:35.搜索插入位置 难易:简单 内容: 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输出: 2 示例 2: 输入: [1,3,5,6], 2 输出: 1 示例 3: 输入: [1,3,5,6], 7 输出: 4 示例 4: 输入: [1,3,5,6], 0 输出: 0 来源:力扣(LeetCode) 链接:https://

【LeetCode-面试算法经典-Java实现】【035-Search Insert Position(搜索插入位置)】

[035-Search Insert Position(搜索插入位置)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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 du

leetcode-35.搜索插入位置

leetcode-35.搜索插入位置 题意 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引.如果目标值不存在于数组中,返回它将会被按顺序插入的位置. 你可以假设数组中无重复元素. 示例 1: 输入: [1,3,5,6], 5 输出: 2 示例 2: 输入: [1,3,5,6], 2 输出: 1 示例 3: 输入: [1,3,5,6], 7 输出: 4 示例 4: 输入: [1,3,5,6], 0 输出: 0 算法 ...没有,实在太简单. code 1 class Solutio

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

[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

搜索插入位置

http://www.lintcode.com/zh-cn/problem/search-insert-position/ 错误点: 在确定了大小为1的区间后,根据插入位置如何返回值,实际有5种情况,可以合并为三种. 位置: start          end 1 2       3     4     5 return  start  start   end  end   end+1 注意点: