Leetcode No.35 Search Insert Position(c++实现)
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++实现)的更多相关文章
- [Leetcode][Python]35: Search Insert Position
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 35: Search Insert Positionhttps://oj.le ...
- 【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 ...
- 【一天一道LeetCode】#35. Search Insert Position
一天一道LeetCode系列 (一)题目 Given a sorted array and a target value, return the index if the target is foun ...
- LeetCode:35. Search Insert Position(Easy)
1. 原题链接 https://leetcode.com/problems/search-insert-position/description/ 2. 题目要求 给定一个已经排好序的数组和一个目标值 ...
- 【LeetCode】35. Search Insert Position 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 二分查找 日期 题目地址:https://leetc ...
- 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 ...
- 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 ...
- [array] leetcode - 35. Search Insert Position - Easy
leetcode - 35. Search Insert Position - Easy descrition Given a sorted array and a target value, ret ...
- 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导致的溢 ...
随机推荐
- java面试一日一题:再谈垃圾回收器中的串行、并行、并发
问题:请讲下java中垃圾回收器的串行.并行.并发 分析:该问题主要考察在垃圾回收过程中垃圾回收线程和用户线程的关系 回答要点: 主要从以下几点去考虑, 1.串行.并行.并发的概念 2.如何考虑串行. ...
- [Django高级之中间件、csrf跨站请求伪造]
[Django高级之中间件.csrf跨站请求伪造] Django中间件 什么是中间件? Middleware is a framework of hooks into Django's request ...
- .NET Core Web API使用HttpClient提交文件的二进制流(multipart/form-data内容类型)
需求背景: 在需要通过服务端请求传递文件二进制文件流数据到相关的服务端保存时,如对接第三方接口很多情况下都会提供一个上传文件的接口,但是当你直接通过前端Ajax的方式将文件流上传到对方提供的接口的时候 ...
- JAVA并发(5)-并发队列LinkedBlockingQueue的分析
本文介绍LinkedBlockingQueue,这个队列在线程池中常用到.(请结合源码,看本文) 1. 介绍 LinkedBlockingQueue, 不支持null,基于单向链表的可选有界阻塞队列. ...
- 3D惯导Lidar SLAM
3D惯导Lidar SLAM LIPS: LiDAR-Inertial 3D Plane SLAM 摘要 本文提出了最*点*面表示的形式化方法,并分析了其在三维室内同步定位与映射中的应用.提出了一个利 ...
- 单核性能强悍,Core i3 这次又要“默秒全”?
单核性能强悍,Core i3 这次又要"默秒全"? 在 Intel 历代酷睿处理器中,定位主流级的 Core i3 一直以超高性价比.低功耗.低发热的特点受到广大用户的青睐,在市场 ...
- Java 面试题关于包装类
这几个问题的知识点涉及的内容非常的刁钻,值得自己好好的理解. 问以下程序的输出结果是: 问题一: Object object=true ? new Integer(1):new Double(2.0) ...
- SpringBoot系列——admin服务监控
前言 springboot项目部署起来后,如何实时监控项目的运行状况呢?本文记录使用springboot-admin对服务进行监控. springboot-admin介绍:https://codece ...
- 【VBA】查找字符串
老婆饼里有老婆吗 Sub test() aaa = "老婆饼里有老婆吗" If InStr(aaa, "老婆") <> 0 Then Debug.p ...
- SpringBoot数据访问(二) SpringBoot整合JPA
JPA简介 Spring Data JPA是Spring Data大家族的一部分,它可以轻松实现基于JPA的存储库.该模块用于增强支持基于JPA的数据访问层,它使我们可以更加容易地构建使用数据访问技术 ...