Given N integers, you are supposed to find the smallest positive integer that is NOT in the given list.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤105). Then N integers are given in the next line, separated by spaces. All the numbers are in the range of int.

Output Specification:

Print in a line the smallest positive integer that is missing from the input list.

Sample Input:

10
5 -25 9 6 1 3 4 2 5 17
 

Sample Output:

7

题意:

给出N个数,判断在这N个数中确实的最小的正整数.

思路:

因为整数是从1开始的,所以缺失的整数最大为N+1,开始的时候将vector数组初始化为-1,给出的数字为正数且小于等于N时,令v[N] = 1.最后遍历V数组,输出第一个遇到的-1的下标.若整个V数组都为1,则输出N+1.

Code:

#include <iostream>
#include <vector> using namespace std; int main() {
int n, t;
cin >> n; vector<int> v(n + 1, -1);
for (int i = 0; i < n; ++i) {
cin >> t;
if (t > 0 && t <= n) v[t] = 1;
}
bool found = false;
for (int i = 1; i <= n; ++i) {
if (v[i] < 0) {
cout << i << endl;
found = true;
break;
}
}
if (!found) cout << n + 1 << endl; return 0;
}

1144 The Missing Number的更多相关文章

  1. PAT 1144 The Missing Number

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  2. PAT 1144 The Missing Number[简单]

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  3. [PAT] 1144 The Missing Number(20 分)

    1144 The Missing Number(20 分) Given N integers, you are supposed to find the smallest positive integ ...

  4. PAT 甲级 1144 The Missing Number (20 分)(简单,最后一个测试点没过由于开的数组没必要大于N)

    1144 The Missing Number (20 分)   Given N integers, you are supposed to find the smallest positive in ...

  5. PAT(A) 1144 The Missing Number(C)统计

    题目链接:1144 The Missing Number (20 point(s)) Description Given N integers, you are supposed to find th ...

  6. PAT 甲级 1144 The Missing Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343463260160 Given N integers, you ...

  7. 1144 The Missing Number (20 分)

    Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...

  8. 1144. The Missing Number (20)

    Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...

  9. Leetcode-268 Missing Number

    #268.  Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find ...

随机推荐

  1. MVCC多版本并发控制器

    在多个事务并发执行的时候,MVCC机制可以协调数据的可见性,事务的隔离级别就是建立在MVCC之上的: MVCC机制通过undo log链和ReadView机制来实现: undo log版本链: 在数据 ...

  2. 面试必备——Java多线程与并发(一)

    1.进程和线程的 (1)由来 1)串行 最初的计算机只能接受一些特定的指令,用户输入一个指令,计算机就做出一个操作.当用户在思考或者输入时,计算机就在等待.显然这样效率低下,在很多时候,计算机都处在等 ...

  3. STL容器整理

    1.vector c++STL中的可变长度数组,主要支持操作有:建立,添加到末尾,返回长度,调整大小,定义迭代器及对迭代器的具体操作.具体如下: 1.建立一个元素类型为int的可变长度数组v,最开始N ...

  4. FakeTaobaoDeepLink - 复制淘宝deeplink来拦截淘宝广告的自动拉起

    Fake Taobao Deeplink 复制 ** com.taobao.tao.welcome.Welcome ** 的intent-filter来拦截误触广告后自动拉起淘宝app 完整工程 Gi ...

  5. 剑指 Offer 60. n个骰子的点数 + 动态规划 + 空间优化

    剑指 Offer 60. n个骰子的点数 Offer_60 题目详情 题解分析 package com.walegarrett.offer; /** * @Author WaleGarrett * @ ...

  6. Java 集合框架 04

    集合框架·Map 和 Collections集合工具类 Map集合的概述和特点 * A:Map接口概述 * 查看API可知: * 将键映射到值的对象 * 一个映射不能包含重复的键 * 每个键最多只能映 ...

  7. CVE-2018-2628-WLS Core Components 反序列化

    漏洞参考 https://blog.csdn.net/csacs/article/details/87122472 漏洞概述:在 WebLogic 里,攻击者利用其他rmi绕过weblogic黑名单限 ...

  8. 推荐一款小众且好用的 Python 爬虫库 - RoboBrowser

    1. 前言 大家好,我是安果! 今天推荐一款小众轻量级的爬虫库:RoboBrowser RoboBrowser,Your friendly neighborhood web scraper!由纯 Py ...

  9. C#开发BIMFACE系列37 网页集成开发1:审图系统中加载模型或图纸

    系列目录     [已更新最新开发文章,点击查看详细] 在之前的<C#开发BIMFACE系列>中主要介绍了BIMFACE平台提供的服务端API接口的封装开发与测试过程. 服务端API测试通 ...

  10. Python-jet后台管理的使用

    python-django-jet库的使用 1.安装 pip install django-jet 2.配置 将'jet'应用添加到你的Django项目的设置文件settings.py中的INSTAL ...