【问题标题】:How to generate row-wise permutations of a 2d Array?如何生成二维数组的逐行排列?
【发布时间】:2022-01-20 18:38:16
【问题描述】:

我最近在一次模拟面试测试中遇到了这个问题(如下所述),但它仍然难倒我:

给定一个二维数组 A,从中生成一个按行排列的一维数组排列列表。

A = [
     [1],
     [5, 2],
     [6]
     ]

Answer: [[1, 5, 2, 6], [1, 6, 5, 2], [5, 2, 1, 6], [5, 2, 6, 1], [6, 1, 5, 2], [6, 5, 2, 1]]

答案解释:我们正在对行进行排列,所以这就像为 [a,b,c] 生成排列,其中每个元素都是可能变化长度的一维数组。

我确信该解决方案涉及回溯,但每当我尝试实施它时,我最终都会得到 5 个以上的参数。我希望你们中的某个人能提供一个优雅的解决方案、伪代码或解释。

【问题讨论】:

  • 生成索引的排列,然后计算出来

标签: algorithm structure array-algorithms


【解决方案1】:

您实际上只是置换行顺序,然后为每个置换展平矩阵。 Python 示例:

from itertools import chain, permutations

def flatten(matrix):
    return list(chain(*matrix))

def permute(matrix):
    return [flatten(perm) for perm in permutations(matrix)]

用你的例子:

>>> M = [[1], [5, 2], [6]]
>>> permute(M)
[[1, 5, 2, 6], [1, 6, 5, 2], [5, 2, 1, 6], [5, 2, 6, 1], [6, 1, 5, 2], [6, 5, 2, 1]]

在另一种语言中,您可能必须实现自己的辅助函数来迭代排列(可能使用递归)和列表展平。

【讨论】:

  • 你不能在面试中真正使用它
  • @PritamBanerjee 我实际上会——它表明您知道如何将问题分解为更小的、已解决的问题,而不是从头开始发明自下而上的解决方案。如果需要,我会编写自己的 permutations()chain() 实现。
【解决方案2】:

在 Java 中,您可以这样做(首先获取所有索引的排列,然后使用它来生成 ans):

public class RowWisePermutation {
    
    public static void main(String[] args) {
        int[][] A = {
                         {1},
                         {5, 2},
                         {6}
        };
        generateRowWisePermutation(A);
    }
    
    public static List<List<Integer>> generateRowWisePermutation(int[][] matrix) {
        List<Integer> temp = new ArrayList<>();
        List<List<Integer>> ans = new ArrayList<>();
        
        List<List<Integer>> indexPermutation = new ArrayList<>();
        int[] indexArr = new int[matrix.length];
        for(int i = 0; i < matrix.length; i++) {
            indexArr[i] = i;
        }
        backtrack(indexArr, temp, indexPermutation);
        
        System.out.println(indexPermutation);
        for(int i = 0; i < indexPermutation.size(); i++) {
            List<Integer> row = new ArrayList<>();
            List<Integer> indices = indexPermutation.get(i);
            for(Integer index : indices) {
               for(int j = 0; j < matrix[index].length; j++) {
                   row.add(matrix[index][j]);
               }
            }
            ans.add(row);
        }
        System.out.println(ans);
        return ans;
            
    }
    
    private static void backtrack(int[] arr, List<Integer> temp, 
            List<List<Integer>> indexPermutation) {
        
        if(temp.size() == arr.length) {
            indexPermutation.add(new ArrayList<>(temp));
            return;
        }
        
        for(int i = 0; i < arr.length; i++) {
            if(temp.contains(i)) { continue; }
            temp.add(arr[i]);
            backtrack(arr, temp, indexPermutation);
            temp.remove(temp.size() - 1);
        }
    }
}

【讨论】:

  • Tyvm,这很有意义!
  • @polarSyndicate 如果可行,请您接受答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-08-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-26
  • 2015-04-22
  • 2022-01-02
  • 2016-01-08
相关资源
最近更新 更多