First Missing Positive
2015-06-13 12:01
225 查看
Given an unsorted integer array, find the first missing positive integer.
For example,
Given
and
Your algorithm should run in O(n) time and uses constant space.
解题思路:坑爹题,今天上午做题不在状态啊,晚上再也不熬夜了,就这leetcode上的水题也卡我半小时。
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.
解题思路:坑爹题,今天上午做题不在状态啊,晚上再也不熬夜了,就这leetcode上的水题也卡我半小时。
class Solution { public: int firstMissingPositive(vector<int>& nums) { int size = nums.size(); if(size == 0) return 1; for(int i = 0; i < size; ++i) { if(nums[i] == i + 1) continue; while(nums[i] > 0 && nums[i] != i + 1 && nums[i]-1 < size && nums[nums[i]-1] != nums[i]) { int t = nums[i]; nums[i] = nums[nums[i]-1]; nums[t-1] = t; cout << nums[i] << " " << nums[nums[i]-1] << endl; } } for(int i = 0; i < size; ++i) { if(nums[i] != i + 1) { return i + 1; } } return size + 1; } };
相关文章推荐
- mysql win源码比较大 不需要的文件删除 记录下来
- 每天睡6小时和8小时区别有多大,看完再也不敢熬夜了
- 微软2014校园招聘笔试试题
- java异常概述
- 九鼎创展s5p4418开发板Android4.4-文件系统编译脚本分析
- beego模板语法 go语言模版语法
- epoll模型的理解封装与应用
- malloc 函数和new的区别
- 关于spring 整合 hibernate 事务不回滚
- Android Handler机制
- C#长链接转短链接(调用新浪api)
- MMC在一个管理单元检测到错误
- DB2常用
- 创始人和VC疯狂撕逼,究竟所为何事?
- 《你的灯亮着吗》阅读笔记(三)
- 使用autotools
- String,StringBuffer与StringBuilder的区别?
- C++标准库中sstream与strstream的区别详细解析
- 学术论文常用英文句型
- Graphviz-Gdot语言学习