博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
41. First Missing Positive
阅读量:4545 次
发布时间:2019-06-08

本文共 1264 字,大约阅读时间需要 4 分钟。

Given an unsorted integer array, find the smallest missing positive integer.

Example 1:

Input: [1,2,0]Output: 3

Example 2:

Input: [3,4,-1,1]Output: 2

Example 3:

Input: [7,8,9,11,12]Output: 1

Note:

Your algorithm should run in O(n) time and uses constant extra space.

 
 AC code:
class Solution {public:    int firstMissingPositive(vector
& nums) { int len = nums.size(); for (int i = 0; i < len; ++i) { while (nums[i] > 0 && nums[i] <= len && nums[nums[i]-1] != nums[i]) { int temp = nums[nums[i]-1]; nums[nums[i]-1] = nums[i]; nums[i] = temp; } } for (int i = 0; i < len; ++i) { if (nums[i] != i + 1) { return i+1; } } return len+1; }};
Runtime: 
0 ms, faster than 100.00% of C++ online submissions for First Missing Positive.

 

桶排序(Bucket sort)或所谓的箱排序,是一个,工作的原理是将分到有限数量的桶里。每个桶再个别排序(有可能再使用别的或是以递归方式继续使用桶排序进行排序)。桶排序是的一种归纳结果。当要被排序的数组内的数值是均匀分配的时候,桶排序使用线性时间({\displaystyle \Theta (n)}{\displaystyle \Theta (n)}())。但桶排序并不是,他不受到{\displaystyle O(n\log n)}{\displaystyle O(n\log n)}下限的影响。

桶排序以下列程序进行:

  1. 设置一个定量的数组当作空桶子。
  2. 寻访序列,并且把项目一个一个放到对应的桶子去。
  3. 对每个不是空的桶子进行排序。
  4. 从不是空的桶子里把项目再放回原来的序列中。

 

转载于:https://www.cnblogs.com/ruruozhenhao/p/9799064.html

你可能感兴趣的文章
在非主线程中创建窗口
查看>>
查看selenium api的方法
查看>>
SQLite
查看>>
在Windows下用gSoap实现简单加法实例
查看>>
小小知识点(二十五)5G关键技术——Massive MIMO(大规模天线阵列)和beamforming(波束成形)...
查看>>
『Collections』namedtuple_具名元组
查看>>
jquery.pagination.js分页插件的运用
查看>>
Windows Phone 7 创建自定义的控件
查看>>
微信公众号模板信息错误过程
查看>>
[转]C# 判断一个字符串是否数字开头
查看>>
JAVA语法——归并排序
查看>>
力扣——第N个泰波那契数
查看>>
服务器 以及HTTP请求的关系
查看>>
JMETER使用
查看>>
如何优化Mysql千万级快速分页,limit优化快速分页,MySQL处理千万级数据查询的优化方案!(zz)...
查看>>
整体性学习的一般顺序 如何进行整体性学习
查看>>
罗永浩简历(自荐新东方的简历)
查看>>
js特效,轻松实现内容的无缝平滑滚动
查看>>
[leetcode]Valid Palindrome
查看>>
LeetCode第四题,Add Two Numbers
查看>>