LeetCode46. Permutations

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_41042404/article/details/88070299

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]

法1.回溯法。递归。每次交换num中的两个数字。第一个数字固定,对后面的数字进行全排列。输出所有全排列数字之后,还原最初的num。再重复第一步:交换第一个数字和后面的数字。

细节:结束条件start==num.size()。每次交换后的num在输出所有全排列之后要还原到最初的num。

class Solution {
public:
    vector<vector<int>> permute(vector<int> &num) {
        vector<vector<int>> ret;
        Helper(num,ret,0);
        return ret;	 
    }

    void Helper(vector<int> num,vector<vector<int>> & ret,int start)
    {
        if(start==num.size())
        {
            //一种全排列
            ret.push_back(num);
        }

        for(int i = start ; i<num.size() ; i++)
        {
            swap(num[i],num[start]);//交换当前
            Helper(num,ret,start+1);//进入下一层布局(后部分全排列)
            swap(num[i],num[start]);//回到上一层布局
        } 
    } 
};

法2.递归。回溯。申请一个空数组out,长度为num大小。从out的第一个空位置开始,在num中选一个数填入out。用数组visited来表示num的元素是否访问过。一直到递归到index=size的时候,打印。每次打印完之后,要回溯到上一位,并且visited恢复为未访问。

class Solution {
public:
    vector<vector<int>> permute(vector<int> &num) { 
        vector<vector<int>> ret;
        if(num.size()==0) return ret;
        vector<int> out,visited(num.size(),0);
        Helper(num,out,ret,0,visited);
        return ret;
    }
    void Helper(vector<int> num,vector<int> & out,
        vector<vector<int>> &ret,int &index,vector<int> &visited)//int index不能用引用,他是const
        {
            if(index == num.size())
            {
                //一次排列完成
                ret.push_back(out);
                return;
            }
            for(int i =0 ;i<num.size();i++)
            {
                if(visited[i]==1)
                    continue;
                visited[i]=1;
                out.push_back(num[i]);

                Helper(num,out,ret,index+1,visited);

                out.pop_back();
                visited[i]=0;
            }
        }
};

细节。 关于index的形参定义:不能用左值引用!因为无法传递常数进去,常数是无法更改的!

void Helper(vector<int> num,vector<int> & out,vector<vector<int>> &ret,int &level,vector<int> &visited);
//定义错误 形参不能设置为左值引用。

Helper(num,out,ret,0,visited);

cannot bind non-const lvalue reference of type 'int&' to an rvalue of type 'int'

0作为常量,只能做右值。所以不能用非常量左值引用作为形参。


 

展开阅读全文

Mr. Young's Picture Permutations

10-15

DescriptionnnMr. Young wishes to take a picture of his class. The students will stand in rows with each row no longer than the row behind it and the left ends of the rows aligned. For instance, 12 students could be arranged in rows (from back to front) of 5, 3, 3 and 1 students. nX X X X XnnX X XnnX X XnnXnnIn addition, Mr. Young wants the students in each row arranged so that heights decrease from left to right. Also, student heights should decrease from the back to the front. Thinking about it, Mr. Young sees that for the 12-student example, there are at least two ways to arrange the students (with 1 as the tallest etc.): n 1 2 3 4 5 1 5 8 11 12nn 6 7 8 2 6 9nn 9 10 11 3 7 10nn12 4nnMr. Young wonders how many different arrangements of the students there might be for a given arrangement of rows. He tries counting by hand starting with rows of 3, 2 and 1 and counts 16 arrangements: n123 123 124 124 125 125 126 126 134 134 135 135 136 136 145 146nn45 46 35 36 34 36 34 35 25 26 24 26 24 25 26 25nn6 5 6 5 6 4 5 4 6 5 6 4 5 4 3 3nnMr. Young sees that counting by hand is not going to be very effective for any reasonable number of students so he asks you to help out by writing a computer program to determine the number of different arrangements of students for a given set of rows.nInputnnThe input for each problem instance will consist of two lines. The first line gives the number of rows, k, as a decimal integer. The second line contains the lengths of the rows from back to front (n1, n2,..., nk) as decimal integers separated by a single space. The problem set ends with a line with a row count of 0. There will never be more than 5 rows and the total number of students, N, (sum of the row lengths) will be at most 30.nOutputnnThe output for each problem instance shall be the number of arrangements of the N students into the given rows so that the heights decrease along each row from left to right and along each column from back to front as a decimal integer. (Assume all heights are distinct.) The result of each problem instance should be on a separate line. The input data will be chosen so that the result will always fit in an unsigned 32 bit integer.nSample Inputnn1n30n5n1 1 1 1 1n3n3 2 1n4n5 3 3 1n5n6 5 4 3 2n2n15 15n0nSample Outputnn1n1n16n4158n141892608n9694845 问答

没有更多推荐了,返回首页