leetcode之缺失的第一个正数
给定一个未排序的整数数组,找出其中没有出现的最小的正整数。
示例 1:
输入: [1,2,0]
输出: 3
示例 2:
输入: [3,4,-1,1]
输出: 2
示例 3:
输入: [7,8,9,11,12]
输出: 1
说明:
你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间。
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/first-missing-positive
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
//这是看了解答后的代码 满足题意 但速度没提高太多
int size = nums.size();
int *numsHash;
int size_hash = size +;
//创建一个哈希表 初始化为0
numsHash = new int[size_hash]();
//遍历nums 对于小于数组长度的元素在哈希表中打个标记
//对于比数组长度大的数据可以忽略,因为第一个缺失的正数没这么大
for (int i = ; i < size; ++i)
{
if (nums[i] > && nums[i] < size_hash)
{
numsHash[nums[i]] = ;
}
}
//遍历哈希表 找出第一个没打标记的
int *end_pos = numsHash + size_hash;
for (int *p = numsHash + ; p != end_pos; ++p)
{
if (*p == )
{
return p - numsHash;
}
}
return size_hash;
}
int firstMissingPositiveNew(vector<int>& nums) {
//这是我自己想的答案,不符合题目O(n)要求,但排名也挺高
//排序 然后找出第一个大于0的数字
//顺序查找下去,直到发现本元数比上一个元素大1就说明找到了
sort(nums.begin(), nums.end());
//从1开始找出第一个不在数组里面的正数 慢
//for (int i = 1; i <= INT_MAX; ++i)
//{
// if (!binary_search(nums.begin(), nums.end(), i))
// {
// return i;
// }
//}
auto it = upper_bound(nums.begin(), nums.end(), );
if (it == nums.end() || *it != ) return ;
for (++it; it != nums.end(); ++it)
{
if ((*it - *(it - )) > )
{
return *(it - ) + ;
}
}
return *(it - ) + ;
}
};
leetcode之缺失的第一个正数的更多相关文章
- LeetCode:缺失的第一个正数【41】
LeetCode:缺失的第一个正数[41] 题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3示例 2: 输入: [3,4,-1,1] ...
- Leetcode 41.缺失的第一个正数
缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: ...
- Java实现 LeetCode 41 缺失的第一个正数
41. 缺失的第一个正数 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: ...
- 【LeetCode】缺失的第一个正数【原地HashMap】
给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8,9,11 ...
- LeetCode 41. 缺失的第一个正数(First Missing Positive)
题目描述 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输入: [7,8 ...
- leetcode 41缺失的第一个正数
time O(n) space O(1) class Solution { public: int firstMissingPositive(vector<int>& nums) ...
- LeetCode缺失的第一个正数
LeetCode 缺失的第一个正数 题目描述 给你一个未排序的整数数组 nums,请你找出其中没有出现的最小的正整数. 进阶:你可以实现时间复杂度为 O(n)并且只使用常数级别额外空间的解决方案吗? ...
- LeetCode(41):缺失的第一个正数
Hard! 题目描述: 给定一个未排序的整数数组,找出其中没有出现的最小的正整数. 示例 1: 输入: [1,2,0] 输出: 3 示例 2: 输入: [3,4,-1,1] 输出: 2 示例 3: 输 ...
- [Leetcode] first missing positve 缺失的第一个正数
Given an unsorted integer array, find the first missing positive integer. For example,Given[1,2,0]re ...
随机推荐
- Redis系列(二):Redis高可用集群
一.集群模式 Redis集群是一个由多个主从节点组成的高可用集群,它具有复制.高可用和分片等特性 二.集群部署 1.环境 3台主机分别是: 192.168.160.146 192.168.160.15 ...
- 常见HTTP请求头和响应头
2. 常用的HTTP请求头 协议头 说明 示例 状态 Accept 可接受的响应内容类型(Content-Types). Accept: text/plain 固定 Accept-Charset 可接 ...
- 程序员的算法课(3)-递归(recursion)算法
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/m0_37609579/article/de ...
- 【Android - 自定义View】之View的measure过程解析
measure(测量)过程是View的工作流程中最开始.最核心的过程,在这个过程中负责确定View的测量宽/高. 对于View和ViewGroup,measure过程有不同的执行方法:如果目标是一个原 ...
- python加载csv数据
入门机器学习时,一些测试数据是网络上的csv文件.这里总结了两种加载csv文件的方式: 1 通过numpy.urllib2加载 import numpy as np import urllib2 ur ...
- idea 使用下Java JDK安装
下载idea 百度云: 链接:https://pan.baidu.com/s/1pmDTH-W1_BhSYJAlcAvljQ 提取码:sgmk 下载Java1.8(jdk-8u181 ...
- SMProxy,让你的数据库操作快三倍!
SMProxy GITHUB:https://github.com/louislivi/smproxy 喜欢请star 中文 | English /$$$$$$ /$$ /$$ /$$$$$$$ /$ ...
- 机器学习笔记(六) ---- 支持向量机(SVM)
支持向量机(SVM)可以说是一个完全由数学理论和公式进行应用的一种机器学习算法,在小批量数据分类上准确度高.性能好,在二分类问题上有广泛的应用. 同样是二分类算法,支持向量机和逻辑回归有很多相似性,都 ...
- vars()
返回一个字典,包含所有在本函数调用时存在的变量
- CSS给元素清除浮动影响的方法,--最全四种方法
代码实例: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...