缺失的数|The Missing Number_1144

描述

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

指定输入

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

指定输出

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

输入样例

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

输出样例

7

分析

其实就是找出连续最小的正数,网上有一种解法,怎么说呢,想法很清奇但也很有效,就是下面这个

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100000 + 10;
int n;
int vis[maxn];
int main() {
    scanf("%d", &n);
    int MAX = -1;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        if (a > 0 && a < maxn) vis[a] = 1;//注意这里
    }
    for (int i = 1; i < maxn; i++) {
        if (vis[i] == 0) {
            printf("%d\n", i);
            break;
        }
    }
 
    return 0;
}

但python很难做到,因为python只有列表(list)类型,很难初始化一个这样庞大的数组(又或许是我队python掌握得不够深),我的想法是下面(本来使用python写的,但一不小心被我删了),只有c++版本,总是有一个错误但是没有搞出来,很气

#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int n;
	cin >> n;
	int in[11000];
	for (int i = 0; i < n; i++) {
		cin >> in[i];
	}
	sort(in, in + n);
	int left_p = 0;
	int right_p = 1;

	for (int i = 0; i < n-1; i++) {
		if (in[left_p] < 0) {
			left_p += 1;
			right_p += 1;
			if (right_p == n) {
				if (in[n - 1] <= 0) {
					cout << 1;
				}
				else
				{
					cout << in[n - 1] + 1;
				}
			}
		}
		else if (in[right_p] - in[left_p] == 1 || in[right_p] - in[left_p] == 0) {
			left_p += 1;
			right_p += 1;
			if (right_p == n) {
				cout << in[left_p] + 1;
				break;
			}
		}
		else
		{
			cout << in[left_p] + 1;
			break;
		}
	}
}
??正文结束??
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值