如何根据列的总和来组织二维数组的列?

huangapple go评论105阅读模式
英文:

How to organize columns of 2D array based on sum of columns?

问题

我想要按每列的和降序排列一个2D数组。例如:

列1的总和是10,列2的总和是11,列3的总和是8。需要将列2和列1按降序排序。更新后的2D数组如下:

这是我用来获取每列总和的代码:

我目前还没有对每列的tempSum进行任何操作。希望能提供一些指导。

英文:

I would like to organize a 2D array in descending order of each columns sum. For example:

  1. 3 1 1
  2. 2 6 5
  3. 5 4 2

The sum of column 1 would be 10, column 2 would be 11, column 3 would be 8. Columns 2 and 1 would need to switch to be sorted in descending order. The updated 2D array would be:

  1. 1 3 1
  2. 6 2 5
  3. 4 5 2

I'm aware of Collections.reverseOrder(), but that only works on sorting 1D arrays in descending order.

Here is the code I am using to get the sum of each column:

  1. int tempSum = 0;
  2. for (int i = 0; i < columns; i++) {
  3. for (int j = 0; j < rows; j++) {
  4. tempSum = array[i][j]
  5. }
  6. //reset tempSum
  7. tempSum = 0;
  8. }

I am currently not doing anything with the tempSum of each column. Any guidance would be great.

答案1

得分: 1

尝试这个。

  1. int[][] transpose(int[][] matrix) {
  2. int rows = matrix.length, cols = matrix[0].length;
  3. int[][] transposed = new int[cols][rows];
  4. for (int r = 0; r < rows; ++r)
  5. for (int c = 0; c < cols; ++c)
  6. transposed[c][r] = matrix[r][c];
  7. return transposed;
  8. }
  9. int[][] m = {
  10. {3, 1, 1, 9},
  11. {2, 6, 5, 4},
  12. {5, 4, 2, 6}};
  13. m = transpose(m);
  14. Arrays.sort(m, Collections.reverseOrder(Comparator.comparingInt(row -> IntStream.of(row).sum())));
  15. m = transpose(m);
  16. for (int[] row : m)
  17. System.out.println("\t" + Arrays.toString(row));

输出

  1. [9, 1, 3, 1]
  2. [4, 6, 2, 5]
  3. [6, 4, 5, 2]

或者如果你需要更高的性能:

  1. m = transpose(m);
  2. m = Arrays.stream(m)
  3. .map(row -> new Object() {
  4. int sum = IntStream.of(row).sum();
  5. int[] origin = row;
  6. })
  7. .sorted((a, b) -> Integer.compare(b.sum, a.sum))
  8. .map(obj -> obj.origin)
  9. .toArray(int[][]::new);
  10. m = transpose(m);
英文:

Try this.

  1. int[][] transpose(int[][] matrix) {
  2. int rows = matrix.length, cols = matrix[0].length;
  3. int[][] transposed = new int[cols][rows];
  4. for (int r = 0; r &lt; rows; ++r)
  5. for (int c = 0; c &lt; cols; ++c)
  6. transposed[c][r] = matrix[r][c];
  7. return transposed;
  8. }

And

  1. int[][] m = {
  2. {3, 1, 1, 9},
  3. {2, 6, 5, 4},
  4. {5, 4, 2, 6}};
  5. m = transpose(m);
  6. Arrays.sort(m, Collections.reverseOrder(Comparator.comparingInt(row -&gt; IntStream.of(row).sum())));
  7. m = transpose(m);
  8. for (int[] row : m)
  9. System.out.println(&quot;\t&quot; + Arrays.toString(row));

output

  1. [9, 1, 3, 1]
  2. [4, 6, 2, 5]
  3. [6, 4, 5, 2]

Or if you need more performance

  1. m = transpose(m);
  2. m = Arrays.stream(m)
  3. .map(row -&gt; new Object() {
  4. int sum = IntStream.of(row).sum();
  5. int[] origin = row;
  6. })
  7. .sorted((a, b) -&gt; Integer.compare(b.sum, a.sum))
  8. .map(obj -&gt; obj.origin)
  9. .toArray(int[][]::new);
  10. m = transpose(m);

huangapple
  • 本文由 发表于 2020年7月28日 09:54:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/63125933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定