矩阵的转置。 Hackerrank 测试用例失败

Transpose of a Matrix. Hackerrank Test Case Fail

我正在努力解决 hackerrank 中的一个相当简单的问题。该问题要求输入一个方阵并打印它的转置。报名参加比赛后,可以在此处找到问题。问题的名称是转置矩阵: https://www.hackerrank.com/contests/csdp-contest/challenges/

我在 Java 代码中对这个问题的解决方案如下:

    import java.io.*;
    public class Solution {
        public static void main(String[] args) throws IOException{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            String line1[] = br.readLine().split(" ");//Read the first line to find the size of the array
            int n = line1.length;
            int ar[][] = new int[n][n];//initialize an integer array with size n
            int rowNumber = 0;
            for (int i=0;i<n;i++)
                ar[rowNumber][i] = Integer.parseInt(line1[i]);//Store the first line in the array
            rowNumber = 1;
            //Store the next input lines in the array if any
            for (int j=0;j<n-1;j++){
                String line[] = br.readLine().split(" ");
                for(int i=0;i<line.length;i++){
                    ar[rowNumber][i] = Integer.parseInt(line[i]);
                }
                rowNumber++;
            }
            //Print the transpose of the matrix by printing ar[j][i] instead of [i][j]
            for(int i=0;i<rowNumber;i++){
                for (int j=0;j<rowNumber;j++){
                    System.out.print(ar[j][i]+" ");
                }
                System.out.println();
            }
        }
    }

上面的代码通过了随问题给出的测试用例,但是当我提交时它没有通过其中一个测试用例。我不明白我是否遗漏了任何边缘情况或代码中是否存在错误。你能帮我解决这个问题吗?

我不熟悉这个测验,但通常数字类型是一个常见的问题(比如 int 算术中的溢出等)。

在这里你似乎没有任何算术问题,但重新阅读问题并检查假设(可能是输入格式?)总是值得的