LeetCode OJ-- First Missing Positive
https://oj.leetcode.com/problems/first-missing-positive/
给一列数,找出缺失的第一个正数。要求时间复杂度 O(n)
第一步遍历一遍,找出最大的数和最小的数。
第二步建立一个vector,以 max+1 为size。
第三部遍历一遍,存储每个存在的数到相应的下标那里。
第四部遍历一遍,寻找数组中第一个计数是0的数。
class Solution {
public:
int firstMissingPositive(int A[], int n) {
int min = ;
int max = ;
//find the smallest and the biggest
for(int i = ;i<n;i++)
{
max = A[i]>max?A[i]:max;
min = min>A[i]?A[i]:min;
}
if(max == )
return ;
if(min == )
return ;
vector<int> map;
map.resize(max+);
for(int i = ;i<n;i++)
{
if(A[i]>)
map[A[i]] = ;
}
for(int i =;i<max;i++)
if(map[i]==)
return i;
return max+;
}
};
LeetCode OJ-- First Missing Positive的更多相关文章
- [array] leetcode - 41. First Missing Positive - Hard
leetcode - 41. First Missing Positive - Hard descrition Given an unsorted integer array, find the fi ...
- 【leetcode】 First Missing Positive
[LeetCode]First Missing Positive Given an unsorted integer array, find the first missing positive in ...
- 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, ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- Java [Leetcode 41]First Missing Positive
题目描述: Given an unsorted integer array, find the first missing positive integer. For example,Given [1 ...
随机推荐
- 用描述符实现classmethod方法和staticmethod方法
1. @classmethod class ClassMethod: def __init__(self, func): self.func = func def __get__(self, inst ...
- Linux系统属性文件详解
1)inode概述 中文意思就是索引节点(index node)第一部分是inode 第二部分是block inode主要用来存放文件属性信息的(也就是ls - l 的结果)包含的属性信息包括文件的大 ...
- Kubernetes添加带Quota限额的CephFS StorageClass
1. 在Ceph上为Kubernetes创建一个文件系统 # ceph osd pool create cephfs_data # ceph osd pool create cephfs_metada ...
- python双向链表的疑问(Question)
Table of Contents 1. 问题 问题 在看 collections.OrderedDict 的源码时,对于它如何构造有序的结构这一部分不是很理解,代码如下: class Ordered ...
- Error:Execution failed for task ':myapplication:processDebugResources'. > com.android.ide.common.pro
Error:Execution failed for task ':myapplication:processDebugResources'. > com.android.ide.common. ...
- play后面加the不加the如何分辨
play表示“参加(某种球类运动或棋牌类的活动)”时,不需要定冠词the,后面直接加球类运动名称或棋牌类活动名称,可根据实际情况翻译成“打,踢,下”等.例如: 1) He often plays fo ...
- 继承Thread类使用多线程
java实现多线程有两种方式,一种是继承Thread类,另外一种就是实现Runnable接口. 两种实现方法的优缺点: 使用Thread类实现多线程局限性就是不支持多继承,因为java是不支持类多继承 ...
- XSS Challenges(1-12关)
XSS Challenges解析: 1. 什么过滤也没有,而且是直接输出.<scrip>alert(document.domain);</script>即可通过 2. 直接输入 ...
- java包、类、方法、属性、常量命名规则
必须用英文,不要用汉语拼音 1:包(package):用于将完成不同功能的类分门别类,放在不同的目录(包)下,包的命名规则:将公司域名反转作为包名.比如www.sohu.com 对于包名:每个字母都需 ...
- c#委托使用
public class StepArgs : EventArgs { public int m_IMax = 0; public int m_IStep = 0; public string m_S ...