你如何找到一个整数二维数组中拥有最多0值的行?

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

How can i find the row which has the most 0 values in an int 2D array?

问题

如何找到具有最多0值的行在一个int二维数组中?

示例:

  1. int[][] arr = new int[4][5];
  2. arr[0][0] = 1;
  3. arr[0][1] = 2;
  4. arr[0][2] = 3;
  5. arr[0][3] = 4;
  6. arr[1][0] = 29;
  7. arr[1][1] = 28;
  8. arr[1][2] = 27;
  9. arr[2][0] = 3;
  10. arr[2][1] = 33;
  11. arr[3][0] = 41;
  12. arr[3][1] = 42;
  13. arr[3][2] = 43;
  14. arr[3][3] = 43;

答案应该是行索引2。

英文:

How can i find the row which has the most 0 values in an int 2D array?

Example:

  1. int[][] arr = new int[4][5];
  2. arr[0][0] = 1;
  3. arr[0][1] = 2;
  4. arr[0][2] = 3;
  5. arr[0][3] = 4;
  6. arr[1][0] = 29;
  7. arr[1][1] = 28;
  8. arr[1][2] = 27;
  9. arr[2][0] = 3;
  10. arr[2][1] = 33;
  11. arr[3][0] = 41;
  12. arr[3][1] = 42;
  13. arr[3][2] = 43;
  14. arr[3][3] = 43;

The answer is should be Row index 2

答案1

得分: 1

以下是翻译好的部分:

有几种方法可以实现你的目标。我会使用 stream 来完成这个任务:

  1. int[][] arr = new int[4][5];
  2. arr[0][0] = 1;
  3. arr[0][1] = 2;
  4. arr[0][2] = 3;
  5. arr[0][3] = 4;
  6. arr[1][0] = 29;
  7. arr[1][1] = 28;
  8. arr[1][2] = 27;
  9. arr[2][0] = 3;
  10. arr[2][1] = 33;
  11. arr[3][0] = 41;
  12. arr[3][1] = 42;
  13. arr[3][2] = 43;
  14. arr[3][3] = 43;
  15. IntStream.range(0, arr.length).boxed()
  16. .map(row -> Map.entry(row, Arrays.stream(arr[row]).filter(i -> i != 0).count()))
  17. .min(Comparator.comparing(Entry::getValue))
  18. .map(Entry::getKey)
  19. .ifPresent(row -> System.out.println("行: " + row));

输出

  1. 行: 2

更新

如果你想使用 for 循环,可以这样做:

  1. int row = -1;
  2. int maxZeros = 0;
  3. for (int i = 0; i < arr.length; i++) {
  4. int zeros = 0;
  5. for (int j = 0; j < arr[i].length; j++) {
  6. if (arr[i][j] == 0) {
  7. zeros++;
  8. }
  9. }
  10. if (zeros > maxZeros) {
  11. maxZeros = zeros;
  12. row = i;
  13. }
  14. }
  15. if (row > -1) {
  16. System.out.println("行: " + row);
  17. }

输出

  1. 行: 2
英文:

There are several ways to achieve your goal. I would use streams for that job:

  1. int[][] arr = new int[4][5];
  2. arr[0][0] = 1;
  3. arr[0][1] = 2;
  4. arr[0][2] = 3;
  5. arr[0][3] = 4;
  6. arr[1][0] = 29;
  7. arr[1][1] = 28;
  8. arr[1][2] = 27;
  9. arr[2][0] = 3;
  10. arr[2][1] = 33;
  11. arr[3][0] = 41;
  12. arr[3][1] = 42;
  13. arr[3][2] = 43;
  14. arr[3][3] = 43;
  15. IntStream.range(0, arr.length).boxed()
  16. .map(row -&gt; Map.entry(row, Arrays.stream(arr[row]).filter(i -&gt; i != 0).count()))
  17. .min(Comparator.comparing(Entry::getValue))
  18. .map(Entry::getKey)
  19. .ifPresent(row -&gt; System.out.println(&quot;Row: &quot; + row));

Output

  1. Row: 2

Update

If you want to use a for loop you can do:

  1. int row = -1;
  2. int maxZeros = 0;
  3. for (int i = 0; i &lt; arr.length; i++) {
  4. int zeros = 0;
  5. for (int j = 0; j &lt; arr[i].length; j++) {
  6. if (arr[i][j] == 0) {
  7. zeros++;
  8. }
  9. }
  10. if (zeros &gt; maxZeros) {
  11. maxZeros = zeros;
  12. row = i;
  13. }
  14. }
  15. if (row &gt; -1) {
  16. System.out.println(&quot;Row: &quot; + row);
  17. }

Output

  1. Row: 2

huangapple
  • 本文由 发表于 2020年7月23日 07:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/63044621.html
匿名

发表评论

匿名网友

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

确定