41. First Missing Positive(困难, 用到 counting sort 方法)
Given an unsorted integer array, find the first missing positive integer.
For example,
Given [1,2,0] return 3,
and [3,4,-1,1] return 2.
Your algorithm should run in \(O(n)\) time and uses constant space.
若数组为[10,20,30]
, 则返回0+1 = 1
, 因为 A[0]!=( 0 + 1 )
.详细情况看下面.
人家解释:
http://www.cnblogs.com/ccsccs/articles/4216113.html
跟Counting sort一样,利用数组的index来作为数字本身的索引,把正数按照递增顺序依次放到数组中。即让A[0]=1, A[1]=2, A[2]=3, ... , 这样一来,最后如果哪个数组元素违反了A[i]=i+1即说明i+1就是我们要求的第一个缺失的正数。
\(O(n)\) time, \(O(1)\) extra space.
代码:
int firstMissingPositive(vector<int>& A) {
const int n = A.size();
// 难度是维护一个 A[i] = i + 1 的数组
// 我们只把小于等于数组长度的并且大于0的并且A[i] != i + 1的元素做调整
for (int i = 0; i < n; i++) {
if (A[i] <= n && A[i] > 0 && A[A[i] - 1] != A[i]) {
int temp = A[A[i] - 1];
A[A[i] - 1] = A[i];
A[i] = temp;
i--; // hard
}
}
// 二次扫描,找出A[i] != i + 1的元素
for (int i = 0; i < n; i++) {
if (A[i] != i + 1)
return i + 1;
}
return n + 1;
}
41. First Missing Positive(困难, 用到 counting sort 方法)的更多相关文章
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- [Leetcode][Python]41: First Missing Positive
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 41: First Missing Positivehttps://oj.le ...
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- LeetCode题解41.First Missing Positive
41. First Missing Positive Given an unsorted integer array, find the first missing positive integer. ...
- 刷题41. First Missing Positive
一.题目说明 题目是41. First Missing Positive,求一个未排序队列中缺失的最小正整数.时间复杂度要求是O(n).难度是Hard,确实难. 二.我的解答 不考虑时间复杂度,首先对 ...
- leetcode 41 First Missing Positive ---java
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- [LeetCode] 41. First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the smallest missing positive integer. Example 1: Input: [1,2, ...
- 41. First Missing Positive
题目: Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2 ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
随机推荐
- SpringCloud是否值得引入?
中小型互联网公司微服务实践-经验和教训 http://xujin.org/sc/sc-zq/#more Spring Cloud在国内中小型公司能用起来吗?https://mp.weixin.qq.c ...
- spring6——AOP的编程术语
面向切面编程作为一种编程思想,允许我们对程序的执行流程及执行结果动态的做出改变,以达到业务逻辑之间的分层管理或者是目标对象方法的增强,spring框架很好的实现了这种编程思想,让我们可以对主业务逻辑和 ...
- 浅显易懂的谈一谈python中的装饰器!!
hello大家好~~我是稀里糊涂林老冷,一天天稀里糊涂的. 前一段时间学习了装饰器,觉着这东西好高大上哇靠!!哈哈,一定要总结一下,方便以后自己查阅,也希望帮助其他伙伴们共同进步! 装饰器: 大家可以 ...
- iOS HTML图片本地预览
引言 相信用过苹果手机的童鞋,会发现很多新闻类的应用,都可以实现HTML图片本地预览,那么这是如何实现的呢?本文将深入阐述其中的原理. 关于此功能,我还实现了一个DEMO,大家可以点击此访问更详细内容 ...
- spring cloud zipkin sleuth与spring boot aop结合后,启动慢
问题描述: 引入了spring cloud的监控starter,间接引入jooq. 又引入了 spring-boot-starter-web,所以间接引入aop. 参考下面资料后:https://gi ...
- 教你用命令行激活win10系统
对于笔者这样爱自己动手的电脑爱好者来说,当然会选择自己组装一台性价比高的台式电脑,一切都准备就绪了,系统也装好了,就差最后一步了--激活系统. 笔者真的很幸运,在网上找到了一些可以使用的密钥,我装的是 ...
- js正则表达式入门以及常见用例
学习正则表达式的最好方法是从例子开始,理解例子之后再自己对例子进行修改,实验.下面给出了不少简单的例子,并对它们作了详细的说明. 假设你在一篇英文小说里查找hi,你可以使用正则表达式hi. 这几乎是最 ...
- requests-认证设置
#如果需要用户名和密码才能登陆网站,则需要认证设置auth=() import requests response = requests.get(url,auth=('user','password' ...
- hasattr(obj,attr) 判断前面是否有后面的属性
hasattr(obj,attr) 判断前面是否有后面的属性
- 合并css 合并图片 合并js
1:合并css 如:index.html 中的代码 <!DOCTYPE html><html lang="en"><head> <me ...