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 Solution {
 2 public:
 3     int searchInsert(vector<int>& nums, int target) {
 4         int index = 0;
 5         for(int i=0; i<nums.size(); i++)
 6         {
 7             if(nums[i] < target)
 8             {
 9                 index++;
10             }
11             else if(nums[i] >= target)
12             {
13                 break;
14             }
15         }
16         return index;
17     }
18 };

原文地址:https://www.cnblogs.com/yocichen/p/10284368.html

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

leetcode-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.搜索插入位置 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()方法的,如果目标值在数组中就很简单 如果没有的话就从头线性扫描一遍,当

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刷题--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题,搜索插入位置

题目概述 题目:力扣: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 81——搜索旋转排序数组 II

1. 题目 2. 解答 2.1. 方法一 基于 LeetCode 33--搜索旋转排序数组 中的方法二. 当 nums[mid] = nums[right] 时,比如 [1, 1, 2, 1, 1],[1, 1, 0, 1, 1],为了找到正确的转折点,我们查看 [mid, right] 之间有没有不等于 nums[mid] 的值,若有,则继续向右查找:否则向左查找. class Solution { public: int Binary_Search(vector<int>& num

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(查找插入位置)

题目链接: https://leetcode.com/problems/search-insert-position/?tab=Description 在给定的有序数组中插入一个目标数字,求出插入该数字的下标 由于该数组是已经排好序的数组,可以利用二分查找. 二分查找的返回结果: 1. 当查找的数字在数组中时,返回第一次出现的下标 2. 当查找的数字不存在时,返回 - pos - 1(即 应当插入位置的相反数再减去 参考代码: package leetcode_50; import java.u