我正在尝试编写一个递归函数来查找排序矩阵中的值。
public static void main(String[] args) {
System.out.println("yeah");
int[][] matrix = {{1, 2, 3, 4, 5},{6, 7, 8, 9, 10},
{11, 12, 13, 14, 15},{16, 17, 18, 19, 20},{21,22,23,24,25}};
printMatrix(matrix);
int[][] testmatrix = {{1,2},{3,4}};
findValue(matrix, 19, 0, 4, 0, 4);
}
//write a print matrix function
public static void printMatrix(int[][] matrix) {
for(int i=0; i<matrix.length; i++) {
for (int j=0; j<matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println("");
}
}
//sorted matrix and value, efficient algo to find value
private static void findValue(int[][] matrix, int k, int rowLow, int rowHigh, int colLow, int colHigh) {
// 1 element left
if ((rowLow==rowHigh) && (colLow==colHigh)) {
if ((matrix[rowLow][colLow])==k) {
System.out.println("position of key: "+rowLow+","+colLow);
return;
}
else {
System.out.println("not found");
return;
}
}
// take care of the case with 2 elements left
else if ((rowLow==rowHigh) && (colLow+1==colHigh)) {
if ((matrix[rowLow][colLow])==k) {
System.out.println("position of key: "+rowLow+","+colLow);
return;
}
else if ((matrix[rowLow][colHigh])==k) {
System.out.println("position of key: "+rowLow+","+colHigh);
return;
}
else {
System.out.println("not found");
return;
}
}
else {
while (rowHigh > rowLow) {
int rowMid = (rowLow+rowHigh)/2;
//check last value of rowMid
if (matrix[rowMid][colHigh] > k) {
findValue(matrix, k, rowLow, rowMid, colLow, colHigh);
}
else {
findValue(matrix, k, rowMid+1, rowHigh, colLow, colHigh);
}
}
while (colHigh > colLow) {
int colMid = (colLow+colHigh) /2 ;
if ((matrix[rowLow][colMid])==k) {
System.out.println("position of key: "+rowLow+","+colMid);
return;
}
else if (matrix[rowLow][colMid] > k) {
findValue(matrix, k, rowLow, rowHigh, colLow, colMid);
}
else {
findValue(matrix, k, rowLow, rowHigh, colMid+1, colHigh);
}
}
}
}
它确实有效,但问题在于它会无限地保持打印位置。
我发现它很奇怪,因为我在打印基础案例中的位置后调用了返回。任何人都可以指出这段代码有什么问题吗?
答案 0 :(得分:0)
我的猜测是你的while循环中有一些错误,导致它永远不会停止执行。在递归调用之后添加返回似乎可以解决问题:
//sorted matrix and value, efficient algo to find value
private static void findValue(int[][] matrix, int k, int rowLow, int rowHigh, int colLow, int colHigh) {
// 1 element left
if ((rowLow==rowHigh) && (colLow==colHigh)) {
if ((matrix[rowLow][colLow])==k) {
System.out.println("position of key: "+rowLow+","+colLow);
return;
}
else {
System.out.println("not found");
return;
}
}
// take care of the case with 2 elements left
else if ((rowLow==rowHigh) && (colLow+1==colHigh)) {
if ((matrix[rowLow][colLow])==k) {
System.out.println("position of key: "+rowLow+","+colLow);
return;
}
else if ((matrix[rowLow][colHigh])==k) {
System.out.println("position of key: "+rowLow+","+colHigh);
return;
}
else {
System.out.println("not found");
return;
}
}
else {
while (rowHigh > rowLow) {
int rowMid = (rowLow+rowHigh)/2;
//check last value of rowMid
if (matrix[rowMid][colHigh] > k) {
findValue(matrix, k, rowLow, rowMid, colLow, colHigh);
return;
}
else {
findValue(matrix, k, rowMid+1, rowHigh, colLow, colHigh);
return;
}
}
while (colHigh > colLow) {
int colMid = (colLow+colHigh) /2 ;
if ((matrix[rowLow][colMid])==k) {
System.out.println("position of key: "+rowLow+","+colMid);
return;
}
else if (matrix[rowLow][colMid] > k) {
findValue(matrix, k, rowLow, rowHigh, colLow, colMid);
return;
}
else {
findValue(matrix, k, rowLow, rowHigh, colMid+1, colHigh);
return;
}
}
}
收率:
yeah
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
position of key: 3,3
答案 1 :(得分:0)
public static void findValue(int[][] matrix, int n, int x){
int i = 0, j = n-1; //set indexes for top right element
while ( i < n && j >= 0 ){
if ( matrix[i][j] == x ){
System.out.println("position of key: " + i + "," + j);
return;
}
if ( matrix[i][j] > x ){
j--;
}else {
i++;
}
}
System.out.println("not found");
}
...
findValue(matrix, 4, 19);
...
输出:
yeah
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25
position of key: 3,3