You are viewing a single comment's thread. Return to all comments →
I read each line as a string then converted the whole thing to a 2D array. This isn't the full code, but the technique works for me
Scanner in = new Scanner(System.in);
int n = in.nextInt();
char charArray = new char[n][n];
for (int i = 0; i < n; i++)
String lines = in.next();
charArray[i] = lines.toCharArray();
for (int i = 1; i < n-1; i++)
for(int j = 1; j < n-1; j++)
if((charArray[i][j] > charArray[i-1][j]) && (charArray[i][j] > charArray[i+1][j]) && (charArray[i][j] > charArray[i][j-1]) && (charArray[i][j] > charArray[i][j+1]))
charArray[i][j] = 'X';
You are assigning a line of string into a 1D array. Then how does it automatically become a 2D array?
The code does not assign the line of string into a 1D array. In the code, charArray is a 2D array (an array of arrays). The line which has "charArray[i] = lines.toCharArray" converts the string to an array of characters, and this array of characters is then stored in charArray[i]. Hence, filling the i-th position of the 2D array.
I did the same thing. My second test case is failing.
Even my second test is failing, I think there is some faulty test scenario.
Failed Test case Info :
Input : first line 1 and second line 9
output : 9
"Input : first line 1 and second line 9 output : 9"
is correct. the first line "1" is n. "9" is the 1x1 cavity map.
Anyone else notice that test case two, given below, is not a square matrix?
The directions definitely specify that it should be an matrix