数组Mex
来源:互联网 发布:数码视讯软件开发 编辑:程序博客网 时间:2024/06/08 13:52
题目描述:
请设计一个高效算法,查找数组中未出现的最小正整数。
给定一个整数数组A和数组的大小n,请返回数组中未出现的最小正整数。保证数组大小小于等于500。
测试样例:
[-1,2,3,4],4
返回:1
AC代码:
class ArrayMex {public: /*void swap(int &t1, int &t2){ int tmp = t1; t1 = t2; t2 = tmp; } */ int findArrayMex(vector<int> A, int n) { // write code here int left=0,right=n,k; while(left<right){ if(A[left]==left+1) left++; else if(A[left]<=left || A[left]>right || A[A[left]-1]==A[left]) A[left]=A[--right]; else{ int tmp = A[left]; A[left] = A[A[left]-1]; A[tmp-1] = tmp;////此处很容易出错,因为A[left]已经改变,不能再使用A[A[left]-1]表达式。 } //swap(A[left],A[A[left]-1]);//建议使用swap函数。 } return left+1; }};
0 0
- 数组Mex
- 数组Mex
- 区间MEX 线段树维护mex数组
- mex
- Mex
- mex
- mex
- MEX文件
- 关于MEX
- Matlab MEX
- E - Mex
- matlab mex
- bzoj3585: mex
- Mex 文件
- 【BZOJ3585】mex
- [bzoj3585]mex
- matlab+mex
- bzoj3585 mex
- 12.View的滑动
- inteliij 14 出现 -Dmaven.multiModuleProjectDirectory system propery is not set. Check $
- 将串口接收到的字符串转化为数值(包含小数)
- form提交多name取值
- linux下 mysql 5.5 数据库主从复制配置
- 数组Mex
- 有关epoll与select机制的剖析
- 2.2.第五个实验--使用数组实现从0到f的显示
- 录音
- NSURLSession的GET和POST请求的封装
- mysql表的垂直拆分和水平拆分
- Android adb shell 报错:more than one device and emulator
- KiFastCallEntry() 机制分析
- 在Eclipse上通过Cygwin编译C++