Leetcode每日一题:41.first-missing-positive(缺失的第一个正数)
int firstMissingPositive(vector<int> &nums)
{//遍历一次数组把大于等于1的和小于数组大小的值放到原数组对应位置,对于重复的元素,置0;//然后再遍历一次数组查当前下标是否和值对应,如果不对应那这个下标就是答案,否则遍历完都没出现那么答案就是数组长度加1。int len = nums.size();for (int i = 0; i < len; i++){if (nums[i] < 1 || nums[i] > len){nums[i] = 0;continue;}if (nums[i] != i + 1){if (nums[nums[i] - 1] < 1 || nums[nums[i] - 1] > len ||nums[nums[i]-1]==nums[i])//不允许重复数字出现nums[nums[i] - 1] = 0;int temp = nums[nums[i] - 1];nums[nums[i] - 1] = nums[i];nums[i] = temp;--i;}}for (int i = 0; i < len; i++){if (nums[i] != i + 1){return i + 1;}}return len + 1;
}
Leetcode每日一题:41.first-missing-positive(缺失的第一个正数)相关推荐
- 41. First Missing Positive 缺失的第一个正数
Title 给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [ ...
- 【排序+难题】LeetCode 41. First Missing Positive
LeetCode 41. First Missing Positive 本博客转载自:[1]http://www.cnblogs.com/grandyang/p/4395963.html [2]htt ...
- [LeetCode]41.First Missing Positive
[题目] Given an unsorted integer array, find the first missing positive integer. For example, Given [1 ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- leetcode每日刷题计划-简单篇day8
leetcode每日刷题计划-简单篇day8 今天是纠结要不要新买手机的一天QAQ想了想还是算了吧,等自己赚钱买,加油 Num 70 爬楼梯 Climbing Stairs class Solutio ...
- Leetcode每日一题:171.excel-sheet-column-number(Excel表列序号)
思路:就是168题的反命题,进制的方式完美解决: Leetcode每日一题:168.excel-sheet-column-title(Excel表名称) class Solution {public: ...
- 【LeetCode每日一题】1723. 完成所有工作的最短时间
[LeetCode每日一题]1723. 完成所有工作的最短时间 [1] 1723. 完成所有工作的最短时间 [2] 473. 火柴拼正方形 [1] 1723. 完成所有工作的最短时间 题目: 给你一个 ...
- leetcode每日一题--雀巢原理;抽屉算法;Quorum机制;分布式应用
leetcode每日一题 539. 最小时间差 示例 1: 输入:timePoints = ["23:59","00:00"] 输出:1 示例 2: 输入:ti ...
- LeetCode每日一题——1812. 判断国际象棋棋盘中一个格子的颜色
LeetCode每日一题系列 题目:1812. 判断国际象棋棋盘中一个格子的颜色 难度:简单 文章目录 LeetCode每日一题系列 题目 示例 思路 题解 题目 给你一个坐标 coordinates ...
最新文章
- 如何在NLP领域干成第一件事
- VC调试篇:减少运行时错误,中断所有异常
- C#“.NET研究”类类型
- 【Android NDK 开发】Android Studio 的 NDK 配置 ( 源码编译配置 | 构建脚本配置 | 打包配置 | CMake 配置 | ndkBuild 配置 )
- mysql校对集_mysql之字符集与校对集
- 数组的定义格式一_动态初始化
- VSCode中怎么改变文件夹的图标
- 数数题(计数类 DP)做题记录
- [剑指Offer] 25.复杂链表的复制
- mysql port range_MySQL 数据库常见调优方法及参数设置_MySQL
- 基于用户的协同过滤推荐算法
- python机器学习入门实例-老司机学python篇:第一季(基础速过、机器学习入门)
- labelimg安装教程
- VS2019+DCMTK3.6.6环境配置
- jq模仿雨滴下落的动画
- 华为CANN训练营笔记——应用开发全流程 [5](with 代码版)
- 云帮平台的设计架构详解
- 微信运动如何读取小米手环的数据
- 合上笔记本屏幕 Ubuntu 20.04 不休眠
- java single threaded_[Java多线程设计模式]读书笔记 - 第一章 Single Threaded Execution