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
/
47. Permutations II.cpp
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
49 lines (46 loc) · 2.41 KB
master
Breadcrumbs
LeetCode
/
47. Permutations II.cpp
Copy path
Top
File metadata and controls
Code
Blame
49 lines (46 loc) · 2.41 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
class Solution {
public:
//在算法1的基础上,当我们枚举第i个位置的元素时
//,若要把后面第j个元素和i交换,则先要保证[i…j-1]范围内没有和位置j相同的元素。
//有以下两种做法
//(1)可以每次在需要交换时进行顺序查找; 刚刚hh
//(2)用哈希表来查重。具体见下面的代码。
//注意不要误以为以下两种做法能够去重:
//(1)最开始先对数组进行排序,以后每次交换时,只要保证当前要交换的元素和前一个元素不同,
//这种做法是错误的,虽然开始进行了排序,但是元素的交换会使数组再次变的无序
//(2)每次进入递归函数permuteRecur时,对从当前索引开始的子数组排序,这种做法也是错误的,
//因为每次交换元素后,我们要恢复交换的元素,如果在递归函数内排序,就不能正确的恢复交换的元素。
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int> >result;
if(nums.empty())
return result;
sort(nums.begin(),nums.end());
permuteHelper(0,nums,result);
return result;
}
void permuteHelper(int start,vector<int>& nums,vector<vector<int> >&result){
if(start==nums.size()){
result.push_back(nums);
return;
}
unordered_set<int>map;
for(int i=start;i<nums.size();i++){
if( map.find(nums[i])==map.end() ){
//实际上,这里的unorder_set只能定义在函数内部,因为如果放在最外面,1 1 1 2 下一次比较的时候1就不能放进去
//放在里面,start+1的时候,如果相同的数字就不会放进去,函数内部连贯的
map.insert(nums[i]);
swap(nums[i],nums[start]);
permuteHelper(start+1,nums,result);
swap(nums[i],nums[start]);
}
}
}
};
/*
if(i>start&&nums[i]==nums[i-1])
continue;
这个是不对的,因为每一次的变换使得数组再次变得无序啊 ,这里说法不对09010-09001-09100-09010
声明一下,不是再次无序,其实不是很准确,应该是 相隔两数相同,会出现两次,还行,马马虎虎
*/
//不会啊
//还是不会啊,完全不会啊 把 001 这个列子多推下就行 001 010 001 不行了 ,忘记插入了
You can’t perform that action at this time.