1. 题目

1.1 英文题目

Given a sorted array of distinct integers 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 must write an algorithm with O(log n) runtime complexity.

1.2 中文题目

给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。

你可以假设数组中无重复元素。

1.3输入输出

输入 输出
nums = [1,3,5,6], target = 5 2
nums = [1,3,5,6], target = 2 1
nums = [1,3,5,6], target = 7 4
nums = [1,3,5,6], target = 0 0
nums = [1], target = 0 0

1.4 约束条件

  • 1 <= nums.length <= 104
  • -104 <= nums[i] <= 104
  • nums contains distinct values sorted in ascending order.
  • -104 <= target <= 104

2. 实验平台

IDE:VS2019

IDE版本:16.10.1

语言:c++11

3. 程序

3.1 测试程序

#include "Solution.h"
#include <vector> // std::vector
#include<iostream> // std::cout
using namespace std; // 主程序
void main()
{
// // 输入
vector<int> nums = { 1,3,5,6 };
int val = 4; Solution solution; // 实例化Solution
int k = solution.searchInsert(nums, val); // 主功能 // 输出
cout << k << endl;
}

3.2 功能程序

3.2.1 最佳程序

(1)代码

#pragma once
#include<vector> // std::vector
using namespace std; //主功能(二分法+递归)
class Solution
{
public:
int searchInsert(vector<int>& nums, int target)
{
int low = 0, high = nums.size()-1;
while(low<=high)
{
int mid = low + (high - low)/2;
if(target == nums[mid])
return mid;
else if(target>nums[mid])
low = mid+1;
else
high = mid-1;
}
return low;
}
};

此程序参考:https://blog.csdn.net/lym940928/article/details/79893316

(2)解读

使用的是二分查找法,当while循环完成时,说明没有找到对应的数字,此时low > high,即low = high+1。因为数字树位于[low, hight+1]中的,也即位于[low,low]之中,因此返回low,即为target的最后的位置。

3.2.2 自写程序

(1)代码

#pragma once
#include<vector> // std::vector
using namespace std; //主功能(二分法+递归)
class Solution
{
public:
int searchInsert(vector<int>& nums, int target)
{
if (target <= nums[0]) return 0; // 如果目标值比数组第一个元素还小,则返回0
else
return func(nums, target, 0, nums.size()); // 运行主要函数
} int func(vector<int>& nums, int target, int left, int right)
{
int min = 0;
if (right - left <= 1) return right;
else
{
int mid = (left + right) / 2;
if (nums[mid] > target)
{
right = mid;
return func(nums, target, left, right); // 递归
}
else if (nums[mid] < target)
{
left = mid;
return func(nums, target, left, right);
}
else
return mid;
}
}
};

(2)思路

由约束条件一可知数组不为空数组,由约束条件三知数组里的元素是按照升序排列的,也就是说不用考虑降序排列的情况。另外由题目中要求时间复杂度为O(log n),可以联想到是用二分法进行求解,二分法的过程中还用到了递归的思想。

4. 相关知识

(1) 报错:error: non-void function does not return a value in all control paths

这个错误在VS2019中没有报,但是在Leetcode那里却报错了,当时挺蒙蔽的,后来查找资料才知道,原来是自己我if...else if...else if...else...里前几个没有return,也就是说有些情况没有返回值,考虑不周全。

这个可以参考:https://blog.csdn.net/pikaqiu123321123/article/details/114580378

Leetcode No.35 Search Insert Position(c++实现)的更多相关文章

  1. [Leetcode][Python]35: Search Insert Position

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...

  2. 【LeetCode】35. Search Insert Position (2 solutions)

    Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...

  3. 【一天一道LeetCode】#35. Search Insert Position

    一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...

  4. LeetCode:35. Search Insert Position(Easy)

    1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...

  5. 【LeetCode】35. Search Insert Position 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...

  6. LeetCode OJ 35. Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  7. LeetCode Problem 35:Search Insert Position

    描述:Given a sorted array and a target value, return the index if the target is found. If not, return ...

  8. [array] leetcode - 35. Search Insert Position - Easy

    leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...

  9. leetcode 704. Binary Search 、35. Search Insert Position 、278. First Bad Version

    704. Binary Search 1.使用start+1 < end,这样保证最后剩两个数 2.mid = start + (end - start)/2,这样避免接近max-int导致的溢 ...

随机推荐

  1. TODO不实现会报错

    kotlin.NotImplementedError: An operation is not implemented: Not yet implemented 会发生--当你 override fu ...

  2. docker部署node.js

    1.dockerfile FROM node:14.16.0 RUN mkdir -p /var/log/lily/ RUN mkdir -p /opt/node # 工作目录 WORKDIR /op ...

  3. CAP 5.1 版本发布通告 - 你期待的 Redis 来了

    前言 今天,我们很高兴宣布 CAP 发布 5.1 版本正式版,在这个版本里我们同样引入了更多令人激动的新特性和改进,同时也得到越来越多人的喜爱. 得益于社区的反馈和贡献者的支持,在过去的两个月里,我们 ...

  4. 『动善时』JMeter基础 — 40、JMeter中ForEach控制器详解

    目录 1.什么是逻辑控制器 2.ForEach控制器介绍 3.ForEach控制器的使用 (1)测试计划内包含的元件 (2)获取学院列表请求内容 (3)JSON提取器内容 (4)ForEach控制器内 ...

  5. 【NX二次开发】Block UI 字符串

    属性说明:     BlockID     String 控件ID     Enable     Logical 是否可操作     Group     Logical 是否分组     Label  ...

  6. noip2013 总结

    转圈游戏 题目 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,-- ...

  7. 如何使用 jest 和 lint-staged 只检测发生改动的文件

    我们现在在推进 EPC 的过程中,单元测试是必备的技能,在本地的 Git commit 之前进行单测非常有必要,总不能把所有的单测的压力都放在流水线上. 毕竟在流水线运行单测的成本还是挺高的,从 pu ...

  8. Django-Auth模块之auth_user表

    一.Auth模块之auth_user表 在创建Django项目之后直接执行数据迁移命令会自动生成许多表. Django在启动之后就可以直接访问admin路由,需要输入用户名和密码,数据参考的就是aut ...

  9. 渗透测试工具Burpsuite操作教程

    Burpsuite简介 Burp Suite 是一款专业的Web和移动应用程序渗透测试工具,是用于攻击web 应用程序的集成平台,包含了许多工具.Burp Suite为这些工具设计了许多接口,以加快攻 ...

  10. MySQL到ClickHouse实时同步-CloudCanal实战

    简述 CloudCanal 近期实现了 MySQL(RDS) 到 ClickHouse 实时同步的能力,功能包含全量数据迁移.增量数据迁移.结构迁移能力,以及附带的监控.告警.HA等能力(平台自带). ...