当前位置:网站首页>LeetCode 41. 缺失的第一个正数
LeetCode 41. 缺失的第一个正数
2022-04-22 19:05:00 【HumbleFool】
41. 缺失的第一个正数

原地Hash
class Solution {
public:
int firstMissingPositive(vector<int>& nums) {
int n = nums.size();
for(int i = 0; i < n; i ++)
{
//原地hash,就是将nums[i] 放到(nums[i] - 1)位置上
//不断的交换位置,最后得到的nums[i]与i + 1不相等的位置就是不存在的
while(nums[i] > 0 && nums[i] <= n && nums[nums[i] - 1] != nums[i])
swap(nums[nums[i] - 1], nums[i]);
}
for(int i = 0; i < n; i ++)
if(nums[i] != i + 1)
return i + 1;
return n + 1;
}
};
版权声明
本文为[HumbleFool]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_45925322/article/details/124340097
边栏推荐
- k9. Chapter 2: install kubernetes v1.0 based on binary package 20 -- basic environment configuration and kernel configuration (I)
- 【Appium踩坑】Failed to capture a screenshot. Does the current view have ‘secure‘ flag set?
- 关于字符串常量池,intern方法的理解
- One piece of data meets all data scenarios? Tencent cloud data Lake solution and DLC kernel technology introduction
- 青训营-刷题打卡-控制并发执行goroutine的数量
- CMS垃圾收集器和G1垃圾收集器
- [best practice] patrol item: turn on URL authentication in content distribution network (CDN)
- MySQL query with serial number
- 网络安全——Burp Suite抓包工具的使用
- Will map () in JS change the original array
猜你喜欢

JSP learning (VIII. JDBC and file upload processing project)

webrtc+turn+peerconnection_server测延时

Error c4996 'fopen': this function or variable may be unsafe Consider using fopen_ s instead. To disabl

The 14th issue of HMS core discovery reviews the long article | enjoy the silky clip and release the creativity of the video

Better than SQL, another domestic database language was born

Introduction to feign, a microservice invocation component

漂亮舒服的KN95口罩,防护能力也很强

Pycharm 配置 Conda,国内使用正确的镜像源地址
![[Luogu] p2372 yyy2015c01 challenge perimeter (BFS)](/img/80/cae4f93f5432450469a07fb6e6e7fc.png)
[Luogu] p2372 yyy2015c01 challenge perimeter (BFS)

校园跑腿平台如何搭建?
随机推荐
XML file input of Chapter 13 of kettle paoding jieniu
运维工具那么多,选择哪个好? 跟着我3秒搞定
Bluetooth shield / cervical massage instrument / mini charger / probe thermometer, segment code LCD display driver ic-vk1088b qfn32 4 * 4 ultra small volume, 22seg * 4com, and can enter the power savi
Research on rocksdb on December 15, 2020
一份数据满足所有数据场景?腾讯云数据湖解决方案及DLC内核技术介绍
微服务调用组件Feign介绍
Simulation experiment of Arduino uno steering gear
【最佳实践】巡检项:内容分发网络(CDN)开启URL鉴权
Simple implementation of linear regression
2019-12-07 wav音频剪切与合并
Take you to understand the principle of highly flexible spark architecture
借《Mastering ABP Framework》好好学学这个框架
Pychar configures CONDA and uses the correct image source address in China
Some indicators in text classification
How to design API interface to realize uniform format return
Fingerprint identification record
Cross chain asset interaction -- how to transfer KSM on Moonriver
C -- database connection statement
Pycharm 配置 Conda,国内使用正确的镜像源地址
漂亮舒服的KN95口罩,防护能力也很强