My code use the same code AC Permutations and Permutations Ⅱ ( use the algorithm of STL nextpermutation)


  • 0
    J

    class Solution {
    public:
    vector<vector<int> > permute(vector<int> &num) {
    vector< vector<int> > re;
    int len = num.size();
    if( len==0 )
    return re;
    if( len==1 )
    {
    re.push_back( vector<int>(1,num[0]) );
    return re;
    }
    sort( num.begin(),num.end() );
    vector<int> t = num;
    re.push_back( t );
    int i,j,k,tval;
    bool exit = true;
    while( 1 )
    {
    exit = true;
    j = len-1;
    while( j!=0 )
    {
    if( num[j] > num[j-1] )
    {
    exit = false;
    i = j-1;
    break;
    }
    j--;
    }
    if( exit )
    break;
    k = len-1;
    while( k!=i )
    {
    if( num[k]>num[i] )
    break;
    k--;
    }
    tval = num[i];
    num[i] = num[k];
    num[k] = tval;
    k = len-1;
    while( j<k )
    {
    tval = num[j];
    num[j] = num[k];
    num[k] = tval;
    ++j,--k;
    }
    vector<int> temp = num;
    re.push_back(temp);
    }
    return re;
    }
    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.