Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
jeannotes
LeetCode
Repository navigation
Code
Issues
Pull requests
1
(1)
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode
/
41. First Missing Positive.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
58 lines (57 loc) · 1.93 KB
master
Breadcrumbs
LeetCode
/
41. First Missing Positive.cpp
Copy path
Top
File metadata and controls
Code
Blame
58 lines (57 loc) · 1.93 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
class Solution {
//https://leetcode.com/problems/first-missing-positive/
public:
int firstMissingPositive(vector<int>& nums) {
// 1-a[0] 2-a[1] 3-a[2] 桶排序
// 注意下面变换顺序
if(nums.size()<1) return 1;
int num;
for(int i=0;i<nums.size();i++){
//if(nums[i]<=0) continue;
num=nums[i];
while(num>0&&num<nums.size()&&num!=nums[num-1]){
swap(nums[num-1],nums[i]);// 这边不能写nums[num]!=num+1 ,因为有可能导致越界啊
num=nums[i];
}//这边似乎就已经全部排列好了 可以 可以啊, 加油 加油啊
}
//转换结束
for(int i=0;i<nums.size();i++){
if(nums[i]!=i+1){
return i+1;
}
}//本来数组就2个数,[2,1],循环结束。
return nums.size()+1;
}
};
//bug-exist 在while那个循环那边 nums[i]>0&&nums[i]<=len 这两个条件没有看到
/*
if (tem>0&&tem<len){
while (tem!=nums[tem-1]){
//3 1 2
swap(nums[i],nums[tem-1]);//2 1 3
tem=nums[i];//1 2 3
}
} 中间循环部分不能写成这个,可能导致转换之后,出现负数,这个时候是一直在转换的,
// 这边不能写nums[num]!=num+1 ,因为有可能导致越界啊
*/
//终于会了一点
// 时间长点,不过能够做出来
// 忘记怎么做了,至于中间那部分,我倒是做出来了
// 竟然一下子做出来了,一下子成功啊,还是不会啊
int firstMissingPositive(vector<int>& nums) {
if (nums.empty())
return 1;
int len = nums.size();
for (int i = 0; i < nums.size();i++) {
int tem = nums[i];
while (tem>0&&tem<len&&nums[i]!=i+1){
// 3 2 1 3
swap(nums[i], nums[tem - 1]);tem = nums[i];
}
}
for (int i = 0; i < nums.size();i++) {
if (nums[i] != i + 1)
return i + 1;
}
return nums.size() + 1;
}// for this situation,when it come to [1,1] ,it was a disaster
You can’t perform that action at this time.