五区的新农民做梦都想吃莴笋,谁卖我点种子?# gardening - 拈花惹草
E*0
1 楼
// 2 4 7 6 3 1
// it1 it2 it3
// switch it1 and it3
// 2 6 7 4 3 1
// sort it2 to end
// 2 6 1 3 4 7
bool nextPermutation(vector &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
//If vector size is 1, permutation is its self.
if (1==num.size())
return true;
vector::iterator rit=num.end();
rit--;
vector::iterator it1,it2,it3;
// From right to left, find the it1 and it2 such that it1 for ( ; rit >= num.begin(); rit-- )
{
it1=rit;
it2=rit;
if (it1==num.begin())
return false;
it1--;
if (*it1break;
}
// From it2 to right, find it3 such that it1 > it3+1 but it1 < it3.
for (it3=it2;it3 {
if (*it3break;
}
it3--;
//switch it1 and it3.
int tmp=*it1;
*it1=*it3;
*it3=tmp;
//sort from it1+1 to end.
sort(++it1,num.end());
return true;
}
// it1 it2 it3
// switch it1 and it3
// 2 6 7 4 3 1
// sort it2 to end
// 2 6 1 3 4 7
bool nextPermutation(vector
// Start typing your C/C++ solution below
// DO NOT write int main() function
//If vector size is 1, permutation is its self.
if (1==num.size())
return true;
vector
rit--;
vector
// From right to left, find the it1 and it2 such that it1
{
it1=rit;
it2=rit;
if (it1==num.begin())
return false;
it1--;
if (*it1break;
}
// From it2 to right, find it3 such that it1 > it3+1 but it1 < it3.
for (it3=it2;it3
if (*it3break;
}
it3--;
//switch it1 and it3.
int tmp=*it1;
*it1=*it3;
*it3=tmp;
//sort from it1+1 to end.
sort(++it1,num.end());
return true;
}