Leetcode全排列问题

本文详细探讨了LeetCode中与全排列相关的题目,包括如何找到一个数字组合的下一个排列,生成所有可能的排列,处理重复数字的全排列,以及找到特定位置的排列序列。讲解了不同的解决方案,如迭代、排序、深度优先搜索等策略。
摘要由CSDN通过智能技术生成

目录

1、编号30 Next Permutation
2、编号44 Permutations
3、编号45 Permutations II
4、编号60 Permutation Sequence

1、编号30 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

寻找当前数字组合的下一个排列。从后往前用排序做。

void nextPermutation (vector<int> &num) {
    int i;
    int j = num.size()-1;
    
    for(i = num.size()-1; i > 0; i--){
        if(num[i-1] < num[i]) {
            while(num[i-1] >= num[j]) j--;
            swap(num[i-1], num[j]);
            sort(num.begin()+i, num.end());
            break;
        }
    }
    
    if(i==0) sort(num.begin(), num.end());
}
    
void swap(int &x, int &y){
    int tmp = x;
    x = y;
    y = tmp;
}

2、编号44 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) {                
        vector<vector<int>> result;  
        vector<int> initElem;
        
        sort(num.begin(), num.end());
        
        initElem.push_back(num[0]);
        result.push_back(initElem);
        for(int i = 1; i < num.size(); i++) result = Insert(result, num[i]);
        return result;                                              
    }                                                           
        
    vector<vector<int> > Insert(vector<vector<int>> result, int num){
        vector<vector<int>> r;
        for(int i = 0; i < result.size(); i++){
            for(int j = 0; j < result[i].size() + 1; j++){
                vector<int> tmp;
                tmp = result[i];
                tmp.insert(tmp.begin()+j, num);
                r.push_back(tmp);
            }
        }
        
        return r;
    }
};

3、编号45 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].

DFS

class Solution {
public:
    vector<vector<int>> permuteUnique(vector<int> &num) {  
        vector<vector<int>> result;  
        vector<int> oneResult; 
        vector<bool> visited(num.size(), 0);  
        
        if(num.size() == 0) return result;  
        
        sort(num.begin(), num.end()); 
        
        GeneratePermute(num, 0, visited, oneResult, result);  
        return result;  
    }  
    void GeneratePermute(vector<int> & num, int level, vector<bool>& visited, 
                vector<int>& oneResult, vector<vector<int> >& result) {  
        if(level == num.size())  {  
            result.push_back(oneResult);  
            return;  
        }  
        for(int i = 0; i< num.size(); i++)  {  
            if(visited[i] == false){  
                if(i > 0 && num[i] == num[i-1] && visited[i-1] == false)  
                    continue;  
                    
                visited[i] = true;  
                oneResult.push_back(num[i]);  
                
                GeneratePermute(num, level+1, visited, oneResult, result);  
                oneResult.pop_back();  
                visited[i] = false;  
            }  
        }  
    }  

4、编号60 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):
"123"
"132"
"213"
"231"
"312"
"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) {
        string resultString;
        vector<int> input;
        for(int i = 0; i < n; i++) input.push_back(i+1);
 
        if(input.size() == 0) return resultString;
        
        k--;/*!!!!!!!!*/
        
        while(input.size() > 1){
            int f = factorization(input.size()-1);
            int pos = k/f;
            resultString += input[pos] + '0';
            input.erase(input.begin() + pos);
            
            /*Update k*/
            k = k%f;
        }
        resultString += input[0] + '0';
        
        return resultString;
    }
    
    int factorization(int n){
        if(n == 0) return 1;
        int r = 1;
        for(int i = n; i > 0; i--) r *= i;
        return r;
    }

};

参考文献:

http://oj.leetcode.com/problems/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值