[LeetCode] Next Permutation、Permutations、Permutations II、Permutation Sequence

Next Permutation:

Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1

用的是STL里面rotate的算法:

class Solution {
public:
    void nextPermutation(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        int n=num.size();
	    if ( n<=1)
		    return;
	    int p=n-2;
	    while(p>=0&&num[p]>=num[p+1])
		    p--;
	    if ( p<0 )
        {
            sort(num.begin(),num.end());
            return;
        }
		    
            
	    int k=p+1;
	    while(k<n&&num[k]>num[p])
		    k++;
	    k--;
	    swap(num[p],num[k]);
	    sort(&num[p+1],&num[n]);
	    return;
    }
};

Permutations:

Given a collection of numbers, return all possible permutations.

For example,
[1,2,3] have the following permutations:
[1,2,3][1,3,2][2,1,3][2,3,1][3,1,2], and [3,2,1]

class Solution {
public:
    vector<vector<int> > permute(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<vector<int> > ans;
	    solve(ans,num,0);
	    return ans;
    }

    void solve(vector<vector<int> >& ans, vector<int>& num,int k)
    {
	    int n =num.size();
	    if ( k>=n )
	    {
		    ans.push_back(num);
		    return;
	    }
	    for(int i=k;i<n;i++)
	    {
		    swap(num[i],num[k]);
		    solve(ans,num,k+1);
		    swap(num[i],num[k]);
	    }
    }
};


Permutations II:

Given a collection of numbers that might contain duplicates, return all possible unique permutations.

For example,
[1,1,2] have the following unique permutations:
[1,1,2][1,2,1], and [2,1,1].

这回是说不能要重复的。

其实如果可以用STL的函数next_permutation的话,一直next_permutation到false,每次把num加到结果里就行了,很简单吧~当然要先sort一下,不过面试时这样答的话,面试官就会让你实现next_permutation了~好在我们前面也做过啊~

另外一个方法就是用一个数组来记录一个数是否出现在一个位置过了

class Solution {
public:
    vector<vector<int> > permuteUnique(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<vector<int> > ans;
	    vector<int> cur;
	    vector<int> used(num.size(),0);
	    sort(num.begin(),num.end());
	    solve(ans,num,cur,used);
	    return ans;
    }
    void solve(vector<vector<int> >& ans,vector<int>& num,vector<int>& cur,vector<int>& used)
    {
	    int n=num.size();
	    if ( cur.size()==n)
	    {
		    ans.push_back(cur);
		    return;
	    }
	    for(int i=0;i<n;i++)
	    {
		    if ( used[i]||(i!=0&&num[i-1]==num[i]&&used[i-1]))
			    continue;
		    cur.push_back(num[i]);
		    used[i]=1;
		    solve(ans,num,cur,used);
		    used[i]=0;
		    cur.pop_back();
	    }
    }
};


另外就是用一个set记录num[i]是否已经放在K这个位置过了。

class Solution {
public:
    vector<vector<int> > permuteUnique(vector<int> &num) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        vector<vector<int> > ans;
	    sort(num.begin(),num.end());
	    solve(ans,num,0);
	    return ans;
    }
    void solve(vector<vector<int> >& ans,vector<int>& num,int k)
    {
	    int n=num.size();
	    if ( k==n)
	    {
		    ans.push_back(num);
		    return;
	    }
	    set<int> used;
	    for(int i=k;i<n;i++)
	    {
		    if ( used.find(num[i])!=used.end())
			    continue;
		    swap(num[i],num[k]);
		    solve(ans,num,k+1);
		    swap(num[i],num[k]);
		    used.insert(num[i]);
	    }
    }
};


Permutation Sequence:

The set [1,2,3,…,n] contains a total of n! unique permutations.

By listing and labeling all of the permutations in order,
We get the following sequence (ie, for n = 3):

  1. "123"
  2. "132"
  3. "213"
  4. "231"
  5. "312"
  6. "321"

Given n and k, return the kth permutation sequence.

Note: Given n will be between 1 and 9 inclusive.

class Solution {
public:
	string getPermutation(int n, int k) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		vector<int> canUse(n,0);
		vector<int> comp(n+1,1);
		for(int i=2;i<=n;i++)
			comp[i]=comp[i-1]*(i-1);
		string ans;
		k--;
		for(int i=n;i>0;i--)
		{
			int t=k/comp[i];
			k=k%comp[i];
			ans.push_back(getUse(canUse,t)+'0');
		}
		return ans;
	}
	int getUse(vector<int>& canUse,int k)
	{
		for(int i=0;i<canUse.size();i++)
		{
			if(canUse[i]==0)
			{
				if(k==0)
				{
					canUse[i]=1;
					return i+1;
				}
				k--;
			}
		}
		return -1;
	}
};


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值