【问题标题】:How to print an unequal row length matrix in spiral form?如何以螺旋形式打印不等行长矩阵?
【发布时间】:2020-07-01 15:50:27
【问题描述】:

我有一个这样的矩阵:

{
  {4,5,6,7,8},
  {2,3,1,4},
  {1,2},
  {1,2,3,4,5}
  {5,6,7,8}
}

我想以螺旋方式打印,所以输出应该是: 4 5 6 7 8 4 2 5 8 7 6 5 1 1 1 3 1 4 3 2

我目前的螺旋矩阵代码是这样的:

void spiral(int a[][]) { 
    int i, k = 0, l = 0, m = a.length, n = a[0].length; 

    while (k < m && l < n) { 
        for (i = l; i < n; ++i) { 
            System.out.print(a[k][i] + " "); 
        } 
        k++; 

        for (i = k; i < m; ++i) { 
            System.out.print(a[i][n - 1] + " "); 
        } 
        n--; 

        if (k < m) { 
            for (i = n - 1; i >= l; --i) { 
                System.out.print(a[m - 1][i] + " "); 
            } 
            m--; 
        }

        if (l < n) { 
            for (i = m - 1; i >= k; --i) { 
            System.out.print(a[i][l] + " "); 
        } 
        l++; 
    } 
} 

问题是我无法理解如何使它适用于不相等的数组长度行。我非常感谢您对此提供的任何帮助和见解。

【问题讨论】:

  • 你的输出有 4 个 1,而你的输入只有 3 个?
  • 嗯。您可以“消耗”这些行,即在打印它们时从数组中删除元素:打印第一行并删除所有元素;打印下面所有行的最后一个元素并删除它们。从右到左打印最后一行;删除所有元素。打印从下到上仍有元素的行的第一个元素。重复。当没有更多要打印的内容时停止。

标签: java arrays algorithm matrix


【解决方案1】:

这是我的解决方案。它可能更有效,但它确实有效:

public static void main(String [] args) throws ScriptException {
    Integer [][] input = new Integer[][] {
                                    {4,5,6,7,8},
                                    {2,3,1,4},
                                    {1,2},
                                    {1,2,3,4,5},
                                    {5,6,7,8}
                                };

   List<List<Integer>> list = listify(input);
   processSpiral(list);
}

private static <T> void processSpiral(List<List<T>> list) {
    if (!list.isEmpty()) {
        printTop(list);
        list.removeIf(l -> l.isEmpty());
        printLast(list);
        list.removeIf(l -> l.isEmpty());
        printBottom(list);
        list.removeIf(l -> l.isEmpty());
        printFirst(list);
        list.removeIf(l -> l.isEmpty());
        processSpiral(list);
    }
}

private static <T> void printFirst(List<List<T>> list) {
    if (!list.isEmpty()) {
        for (int i = list.size() -1; i >= 0; i --) {
            List<T> row = list.get(i);
            if (!row.isEmpty()) {
                System.out.print(String.format("%s ", row.get(0).toString()));
                row.remove(0);
            }
        }
    }
}

private static <T> void printBottom(List<List<T>> list) {
    if (!list.isEmpty()) {
        List<T> bottomRow = list.get(list.size() - 1);
        Collections.reverse(bottomRow);
        for (T i : bottomRow) {
            System.out.print(String.format("%s ", i.toString()));
        }
        list.remove(list.size() - 1);
    }
}
private static <T> void printLast(List<List<T>> list) {
    list.stream()
        .filter(l -> !l.isEmpty())
        .forEach(l -> {
            T i = l.get(l.size() - 1);
            System.out.print(String.format("%s ", i.toString()));
            l.remove(l.size() - 1);
        });
}
private static <T> void printTop(List<List<T>> list) {
    List<T> firstRow = list.get(0);
    list.remove(0);
    //print the first row
    for (T i : firstRow) {
        System.out.print(String.format("%s ", i.toString()));
    }
}

private static <T> List<List<T>> listify(T [][] input) {
    //convert to List of List for simplicity's sake
    List<List<T>> list = new ArrayList<>();
    for (T [] row : input) {
        //do not use Arrays.asList() as that creates an immutable List
        List<T> l = new ArrayList<>(row.length);
        for (T i : row) {
            l.add(i);
        }
        list.add(l);
    }

    return list;
}

【讨论】:

  • 请不要只发布代码作为答案,而是解释您的代码的作用以及它如何解决问题的问题。带有解释的答案通常质量更高,更有可能吸引投票。
猜你喜欢
  • 2018-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-10
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多