我正在尝试在线解决一些面试问题。我需要帮助理解这一行。 board[i][j]='#';
有人可以向我解释一下吗?感谢
public static boolean exist(char[][] board, String word) {
int m = board.length;
int n = board[0].length;
boolean result = false;
for(int i=0; i<m; i++){
for(int j=0; j<n; j++){
if(dfs(board,word,i,j,0)){
result = true;
}
}
}
return result;
}
public static boolean dfs(char[][] board, String word, int i, int j, int k){
int m = board.length;
int n = board[0].length;
if(i<0 || j<0 || i>=m || j>=n){
return false;
}
if(board[i][j] == word.charAt(k)){
char temp = board[i][j];
board[i][j]='#';
if(k==word.length()-1){
return true;
}else if(dfs(board, word, i-1, j, k+1)
||dfs(board, word, i+1, j, k+1)
||dfs(board, word, i, j-1, k+1)
||dfs(board, word, i, j+1, k+1)){
return true;
}
board[i][j]=temp;
}
return false;
}
答案 0 :(得分:3)
'board'是一个二维数组。每次迭代板以避免重新访问已访问过的节点时,它们已经为访问节点分配了一些值。 (即。[i] [j] =“#”)。 您可以在“#”(仅表示其访问过)中使用任何字符
答案 1 :(得分:0)
在java中你几乎没有数组的例子:o)
因此,
的数组变量的board[i][j]='#'
只是将#
设置为名为[i,j]
board
位置
//one dimensional array with 10 values
char[] oneDimArray = new char[10];
for (int i = 0; i < oneDimArray.length; i++) {
//i works as index, set value at index "i" as follow- ascii table "48" equals to zero :o)
oneDimArray[i]= (char) (48+i);
}
for (int i = 0; i < oneDimArray.length; i++) {
//i works as index, actual value you will get as follow
System.out.println("value @ " + i + ": "+ oneDimArray[i]+ " ");
}
System.out.println("===");
//two dim array - i x j = 10x5 values
String[][] twoDimArray = new String[10][5];
//iterate over first index- 'i' - eg. row
for (int i = 0; i < twoDimArray.length; i++) {
//iterate over second index- 'j' - eg. column
for (int j = 0; j < twoDimArray[i].length; j++) {
//fill @ index [i,j]
twoDimArray[i][j] = "[" + i + ";" + j+"]";
}
}
for (int i = 0; i < twoDimArray.length; i++) {
//iterate over second index- 'j' - eg. column
for (int j = 0; j < twoDimArray[i].length; j++) {
//and read
System.out.println("value @ " + i +"," + j + ": " + twoDimArray[i][j]);
}
System.out.println();
}
您将获得如下输出:
value @ 0: 0
value @ 1: 1
value @ 2: 2
value @ 3: 3
value @ 4: 4
value @ 5: 5
value @ 6: 6
value @ 7: 7
value @ 8: 8
value @ 9: 9
===
value @ 0,0: [0;0]
value @ 0,1: [0;1]
value @ 0,2: [0;2]
value @ 0,3: [0;3]
value @ 0,4: [0;4]
value @ 1,0: [1;0]
value @ 1,1: [1;1]
value @ 1,2: [1;2]
value @ 1,3: [1;3]
value @ 1,4: [1;4]
value @ 2,0: [2;0]
value @ 2,1: [2;1]
value @ 2,2: [2;2]
value @ 2,3: [2;3]
value @ 2,4: [2;4]
value @ 3,0: [3;0]
value @ 3,1: [3;1]
value @ 3,2: [3;2]
value @ 3,3: [3;3]
value @ 3,4: [3;4]
value @ 4,0: [4;0]
value @ 4,1: [4;1]
value @ 4,2: [4;2]
value @ 4,3: [4;3]
value @ 4,4: [4;4]
value @ 5,0: [5;0]
value @ 5,1: [5;1]
value @ 5,2: [5;2]
value @ 5,3: [5;3]
value @ 5,4: [5;4]
value @ 6,0: [6;0]
value @ 6,1: [6;1]
value @ 6,2: [6;2]
value @ 6,3: [6;3]
value @ 6,4: [6;4]
value @ 7,0: [7;0]
value @ 7,1: [7;1]
value @ 7,2: [7;2]
value @ 7,3: [7;3]
value @ 7,4: [7;4]
value @ 8,0: [8;0]
value @ 8,1: [8;1]
value @ 8,2: [8;2]
value @ 8,3: [8;3]
value @ 8,4: [8;4]
value @ 9,0: [9;0]
value @ 9,1: [9;1]
value @ 9,2: [9;2]
value @ 9,3: [9;3]
value @ 9,4: [9;4]