题目描述:
输入一个字符串,打印出该字符串中字符的所有排列。
你可以以任意顺序返回这个字符串数组,但里面不能有重复元素。
示例:
输入:s = "abc"
输出:["abc","acb","bac","bca","cab","cba"]来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/zi-fu-chuan-de-pai-lie-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
分析:真他妈就是一杯茶一根烟,一道leetcode写一天。。。。
看着这么简单,但是对于递归很差的我来说,还是有很大难度的。。。
话不多说,分析,我们可以看到,abc的排列组合,可以划分为以a开头的,以b开头的,以c开头的,即:
abc acb
bac bca
cab cba
假定以a开头的为例,后两位bc和cb,又可以看作是分别以b和c(去掉a以外的所有字母)开头的,以此类推,便可以写出递归函数,该递归函数每次确定一位,后面的组合通过递归给出,即
s=a+digui(bc),可得到abc、acb。这里面也涉及了回溯的思想。
代码如下:
class Solution {
public:
vector<string> permutation(string s) {
vector<string> res;
// 递归,得到含有重复字符串的排列组合
res=digui(s);
// 用hash表map记录下所有字符串出现的次数,本质是去除重复字符串
int l=res.size();
unordered_map<string,int> map;
for(int i=0;i<l;i++)
{
if(map.find(res[i])!=map.end())
map[res[i]]++;
map.insert(make_pair(res[i],1));
}
// 把hash表中的所有节点取出来,赋给norepeatres,则norepeatres中不含有重复字符串
vector<string> norepeatres;
auto it=map.begin();
for(;it!=map.end();it++)
norepeatres.push_back(it->first);
return norepeatres;
}
vector<string> digui(string s)
{
vector<string> res;
// 假设s="abc",
// 可以看到所有排列组合为:
// axx
// axx
// bxx
// bxx
// cxx
// cxx
// 则第一位依次放a,b,c,
// 所有a开头的,可以通过递归得到结果a+digui(s),即
// abx
// acx
// 这样每次确定一位,通过递归确定后面的组合,即可得到答案
for(int i=0;i<s.length();i++)
{
string temp;
if(s[i]==' ')
continue;
char tihuan=s[i];
s[i]=' ';
temp+=tihuan;
vector<string> sub=digui(s);
if(sub.size()==0)
res.push_back(temp);
for(int k=0;k<sub.size();k++)
res.push_back(temp+sub[k]);
s[i]=tihuan;
}
return res;
}
};
几个注意点:
1.hash表map的创建: unordered_map<string,int> map;
2.hash表的查找:if(map.find(res[i])!=map.end())
3.hash表的插入:map.insert(make_pair(res[i],1));
4.hash表的遍历:auto it=map.begin();
for(;it!=map.end();it++)
5.hash表的第一第二个元素:it->first;if->second;
至于c++的next_permutation函数就不再说了,这道题考查的不是这个,有兴趣的自行百度。
算了,还是联系一下next_permutation吧,以防忘记
class Solution {
public:
vector<string> permutation(string s) {
vector<string> res;
sort(s.begin(),s.end());
do{
res.push_back(s);
}while(next_permutation(s.begin(),s.end()));
return res;
}
};
几个注意点:
1.string类型用sort进行排序,两个参数分别为s.begin()和s.end()
看了题解,这也是一个深搜的题目,可以用深搜的思路去做。
附上代码:
class Solution {
public:
vector<string> permutation(string s)
{
vector<string> res;
dfs(res,s,"");
// 去除重复元素
// 先排序
sort(res.begin(),res.end());
// unique划分无重复元素
auto it=unique(res.begin(),res.end());
// 删除重复元素
res.erase(it,res.end());
return res;
}
//找到所有的排列组合
void dfs(vector<string> &res,string s,string str)
{
// 找到结果并存储
if(str.length()>=s.length())
{
res.push_back(str);
return;
}
for(int i=0;i<s.length();i++)
{
// 如果当前字母没有被使用
if(s[i]!=' ')
{
// str进行累加,s[i]标注已使用
char temp=s[i];
str+=s[i];
s[i]=' ';
//str更新后 进入下一次的dfs
dfs(res,s,str);
// s[i]恢复
s[i]=temp;
// str去除尾元素
str.erase(str.end()-1);
}
}
}
};
几个注意点:
1.删除string中的元素:erase
erase一共三种用法:
1.erase(pos,n);
删除从下标pos开始的n个字符,比如erase(0,1)就是删除第一个字符
2.erase(position);
删除postion处的一个字符(position是一个string类型的迭代器)
3.erase(first,last)
删除从first到last之间的字符(first和last都是迭代器)
————————————————
版权声明:本文为CSDN博主「爱编程的小屁孩」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/fyf18845165207/article/details/82729704
2.删除string中重复元素 :unique
算法unique能够移除重复的元素。每当在[first, last]内遇到有重复的元素群,它便移除该元素群中第一个以后的所有元素。
注意:unique只移除相邻的重复元素,如果你想要移除所有(包括不相邻的)重复元素,必须先将序列排序,使所有重复元素都相邻。
unique会返回一个迭代器指向新区间的尾端,新区间之内不包含相邻的重复元素。
事实上unique并不改变元素个数,有一些残余数据会留下来,可以用erase函数去除。
————————————————
版权声明:本文为CSDN博主「线上幽灵」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chen134225/article/details/81346689
eg:
vector<int> res = { 2, 3, 4, 5, 2, 3, 4, 5 };
sort(res.begin(), res.end());
vector<int>::iterator ite = unique(res.begin(), res.end());
res.erase(ite, res.end());
————————————————
版权声明:本文为CSDN博主「线上幽灵」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/chen134225/article/details/81346689