class Solution {
public:
vector<vector<int> > permute(vector<int> &num) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<int> >ans;
sort(num.begin(),num.end());
do
ans.push_back(num);
while(nextPermute(num));
return ans;
}
bool nextPermute(vector<int> &num)
{//5、7、9、8、6
for(int i=num.size()-2;i>=0;i--)
{
if(num[i]<num[i+1])
{
for(int j=i+1;j<num.size();j++)
{
if(j==num.size()-1||num[j+1]<=num[i])
{
swap(num[i],num[j]);
reverse(num.begin()+i+1,num.end());
return true;
}
}
}
}
reverse(num.begin(),num.end());
return false;
}
};
【leetcode】Permutations
最新推荐文章于 2019-05-10 11:15:43 发布