code1:
class Solution {
public:
void nextPermutation(vector<int> &num) {
next_permutation(num.begin(),num.end());
REP(i,0,num.size())cout<<num[i]<<endl;
}
};
code2:
手动实现
<pre name="code" class="cpp">class Solution {
public:
void nextPermutation(vector<int> &num) {
int min=INF;
int select=-1;
int len=num.size();
REPD(i,len-2,-1)
{
REP(j,i+1,len)
if (num[j]>num[i]&&min>num[j])
{
select = j;
min = num[j];
}
if (select != -1)
{
swap(num[i],num[select]);
sort(num.begin()+i+1,num.end());
break;
}
}
if (select == -1)
sort(num.begin(),num.end());
}
};