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-cn.com/problems/search-insert-position

第一次思路

遍历所有元素,当该元素大于大于目标值时,将该元素的索引返回即可

Code

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int i;
        for(int i(0);i<nums.size();i++){
            if(nums[i]==target)
                return i;
            if(nums[i]>target)
                {
                    return i;
                    break;
                }

        }
        if(nums[nums.size()-1]<target)
            return i;
        return i;
    }
};

测试 Submit

分析

该方法需要遍历所有元素,使用时间较长

改进

使用二分法
将数组分为左右两个区间,将目标值与中间位置的元素进行比较大小,确定左右两个区间。

改进Code

class Solution {
public:
    int searchInsert(vector<int>& nums, int target) {
        int mid=0;
        int left=0;
        int right=nums.size()-1;
        while(left<right)
        {
            mid=(right-left)/2+left;
            if(target>nums[mid])
            {
                left=mid+1;
            }
            else if(target<nums[mid])
            {
                right=mid-1;
            }
            else return mid;
        }
        if(target<=nums[left])//插入位置在开头的情况
            return left;
        return left+1;//插入位置在结尾的情况
    }
};

改进Submit

收获总结

二分法要比全部遍历方便的多,要多多使用

原文地址:https://www.cnblogs.com/HanLongfeng/p/12045509.html

时间: 2024-10-08 12:55:59

leetCode 第35题,搜索插入位置的相关文章

[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第35题--Valid Sudoku

题目:Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note:A valid Sudoku board (partial

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

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.ou

【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] 系统刷题5_Dynamic Programming

Dynamic Programming 实际上是[LeetCode] 系统刷题4_Binary Tree & Divide and Conquer的基础上,加上记忆化的过程.就是说,如果这个题目实际上是类似于Divide and conquer或者说是DFS,但是在计算过程中有很多重复计算同样的过程的话,那么就可以用Dynamic prgramming/记忆化搜索来完成.基本就是利用空间来简化时间复杂度的过程. 可以/很有可能使用Dynamic programming的条件,满足之一即可. 1.

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是