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. 友盟+U-APM应用性能报告:Android崩溃率达0.32%,OPPO 、华为、VIVO 崩溃表现良好

    ​随着信息技术高速发展,移动互联几乎已成为了一种生活方式的代名词,在全民上网的数字热潮中,如何能最大程度保障产品服务的稳定性,提供良好的用户体验,是当前企业都需要思考和亟待解决的问题.App的应用性能 ...

  2. redis 处理缓存穿透

    1. 缓存穿透简述 举例说明,redis中确实没有key值为"redis"数据,并且数据库里面也没有,那么每一次都会穿过缓存层,会将请求打到数据库查询,然后数据库进行查询,造成了不 ...

  3. ssh创建与添加密钥开启免密登陆 免确认机器指纹参数

     主要是两个步骤 1.控制主机创建密钥对(私钥和公钥) 2.把密钥对的公钥加入对方的认证列表中 [root@vps ~]# ssh-keygen [root@vps ~]# ssh-copy-id u ...

  4. 利用MathType快速提取论文中的公式

    首先随便打开一个论文,比如下图,我们想提取公式(2.2.7) 第一步:按截图快捷键:Win+Shift+S ,把公式截取下来 第二步:打开大佬开发的网站:https://mathf.itewqq.cn ...

  5. Django中数据库操作相关的错误

    问题:字段修改属性发生错误 1> >python manage.py makemigrations You are trying to add a non-nullable field ' ...

  6. 【补档_STM32单片机】脉搏波采集显示硬件设计

    一.脉搏波简介 ​ 脉搏一般情况下指的都是动脉脉搏.每分钟的脉搏次数称为脉率,正常情况下与心率是一致的.心脏的一次收缩和舒张成为一个心动周期.在每个心动周期内,心室的收缩和舒张会引起脉内压力的周期性波 ...

  7. Java常用集合笔记

    最近事情比较少,闲暇之余温习巩固一下Java的一些基础知识,并做一些笔记, Java常用集合, 主要参考的这篇文章:Java常用集合 ArrayList/Vertor 1. ArrayList 的主要 ...

  8. 内建函数 iter()

    可以选择接受一个无参的可调用对象以及一个哨兵(结束)作为输入.当已这种方式使用时,iter()会创建i一个迭代器,然后重复调用用户提供的可调用对象,直到返回哨兵的值为止. import sys wit ...

  9. 2021.5.22 noip模拟1

    这场考试考得很烂 连暴力都没打好 只拿了25分,,,,,,,,好好总结 T1序列 A. 序列 题目描述 HZ每周一都要举行升旗仪式,国旗班会站成一整列整齐的向前行进. 郭神作为摄像师想要选取其中一段照 ...

  10. 给powershell增加类似于linux的alias功能

    给powershell增加类似于快捷方式的功能(类似于linux的alias) 首先执行 set-executionpolicy remotesigned 允许powershell执行脚本 然后执行e ...