leetcode_41. First Missing Positive_cyclic swapping
https://leetcode.com/problems/first-missing-positive/
给定一个长度为len的无序数组nums,找到其第一个丢失的正整数。
解法:
使用cyclic swapping algorithm。将满足条件 0 < num <= nums.size()的num放到下标为num的位置上,最终第一个nums[i]!=i的i即是最小的丢失的正整数。
代码将num放在下标为num-1的位置,思想是一样的。
注意交换的条件,避免死循环。
class Solution
{
public:
int firstMissingPositive(vector<int>& nums)
{
int res=;
for(int i=; i<nums.size(); i++)
while(nums[i]> && nums[i]<=nums.size() && nums[i]!=i+ && nums[nums[i]-]!=nums[i])
swap(nums[i], nums[nums[i]-]);
for(int i=;i<nums.size();i++)
if(nums[i]!=i+)
return i+;
return nums.size()+;
}
};
leetcode_41. First Missing Positive_cyclic swapping的更多相关文章
- LeetCode: First Missing Positive 解题报告
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- [Algorithm] Find first missing positive integer
Given an array of integers, find the first missing positive integer in linear time and constant spac ...
- 【spring boot】整合LCN,启动spring boot2.0.3 启动报错:Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
spring boot 2.0.3启动报错: Error starting ApplicationContext. To display the conditions report re-run yo ...
- cyclic swapping algorithm
原文见:https://leetcode.com/problems/couples-holding-hands/discuss/113362/JavaC%2B%2B-O(N)-solution-usi ...
- error C4430:missing type specifier 解决错误
错误 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...
- Missing Push Notification Entitlement 问题
最近打包上传是遇到一个问题: 描述: Missing Push Notification Entitlement - Your app includes an API for Apple's Push ...
- PHPmailer关于Extension missing: openssl报错的解决
最近在写一个网页的时候,需要用到PHPmailer来发送邮件,按照官网上给出的demo写出一个例子,却报错Extension missing: openssl 最后发现需要修改php.ini中的配置: ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
随机推荐
- [Selenium] 操作浏览器 Cookies
WebDriver 提供了一系列 Cookies 的操作来获取.填写.删除 Cookies 的方法,节省了多次在登陆页面的查找元素并填写登录信息的时间. 1)获取 Cookies ,并保存到文件中以备 ...
- 「LuoguP1496」 火烧赤壁
Description 曹操平定北方以后,公元208年,率领大军南下,进攻刘表.他的人马还没有到荆州,刘表已经病死.他的儿子刘琮听到曹军声势浩大,吓破了胆,先派人求降了. 孙权任命周瑜为都督,拨给他三 ...
- Cow Marathon(树的直径)
传送门 Cow Marathon Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 5362 Accepted: 2634 ...
- mongoDB的复制集5----复制集安全(认证,用户,权限)
一.什么是认证 如何开启认证 1).auth=true(在配置文件里增加) 2).keyFile(建议添加到配置文件里) #如果设置了auth=true,但第一次没有创建用户就启动实例怎 ...
- 【转】构建Maven项目自动下载jar包
原文地址:https://blog.csdn.net/gfd54gd5f46/article/details/54973954 使用Maven 自动下载jar包 右键单击项目,将项目 转换成Maven ...
- JS按字节截取字符长度实例
处理过长的字符串,截取并添加省略号 * 注:半角长度为1,全角长度为2 * * pStr:字符串 * pLen:截取长度 * * return: 截取后的字符串 * 代码如下: function ...
- Cocoapods fatal: Remote branch #{s.version} not found in upstream origin
遇到一个错误: fatal: Remote branch #{s.version} not found in upstream origin 解决办法 网上搜了很多,都无效 可能仅适用我的情况,分享出 ...
- Mac系统下源码编译安装MySQL 5.7.17
1.下载并解压到:/Users/xiechunping/Softwares/mysql-5.7.17下载地址:http://ftp.ntu.edu.tw/pub/MySQL/Downloads/MyS ...
- 【Codeforces Round #411 (Div. 1)】Codeforces 804C Ice cream coloring (DFS)
传送门 分析 这道题做了好长时间,题意就很难理解. 我们注意到这句话Vertices which have the i-th (1 ≤ i ≤ m) type of ice cream form a ...
- P3007 [USACO11JAN]大陆议会The Continental Cowngress(2-SAT)
简述:给出 n 个法案, m 头牛的意见, 每头牛有两个表决 格式为 “支持或反对某法案”, 每头牛需要至少满足一个表决, 不可能成立的话输出 IMPOSSIBLE, 否则输出方案, Y代表能, N代 ...