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 ...
随机推荐
- ELK学习总结(2-3)Mget获取多个文档
mget 获取多个文档 1.curl 命令格式:mget获取多个文档: curl 'localhost:9200/_mget' -d '{ "docs":[ { " ...
- LXC学习实践(3)快速体验第一个容器
1.搭建第一个 LXC 虚拟计算机 #yum install lxc* 2.安装软件包后要检查 Linux 发行版的内核对 LXC 的支持情况,可以使用下面命令 #lxc-checkconfig #l ...
- C#程序编写规范
代码书写规则 1.尽量使用接口,然后使用类实现接口,提高程序的灵活性. 2.一行不要超过80个字符. 3.尽量不要手工更改计算机生成的代码,若必须要改,一定要改为和计算机生成的代码风格一样. 4.关键 ...
- margin-top塌陷
margin-top 塌陷 在两个不浮动的盒子嵌套时候,内部的盒子设置的margin-top会加到外边的盒子上,导致内部的盒子margin-top设置失败,解决方法如下: 1.外部盒子设置一个边框: ...
- 初学Java Web(9)——学生管理系统(简易版)总结
项目开始时间:2018年4月8日14:37:47 项目完成时间:2018年4月9日10:03:30 技术准备 这个项目是自己用于巩固 J2EE 相关知识的练手项目,非常简单,但是相关的功能却非常实用, ...
- 【转】Python3中urllib详细使用方法(header,代理,超时,认证,异常处理)
urllib是python的一个获取url(Uniform Resource Locators,统一资源定址器)了,我们可以利用它来抓取远程的数据进行保存哦,下面整理了一些关于urllib使用中的 ...
- [LeetCode] Minimum Window Subsequence 最小窗口序列
Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...
- [LeetCode] Valid Triangle Number 合法的三角形个数
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- IIS&ASP.NET 站点IP跳转到域名
前言:先到微软的 https://www.iis.net/downloads/microsoft/url-rewrite 下载URL Rewrite 目标:输入ip跳转到域名所在的网站 比如58的1 ...
- Mysql之视图的操作
视图的操作: 1.视图的创建: create view view_name as 查询语句; 2.视图的查看: show tables;// 显示所有的表和视图 show create view vi ...