【发布时间】:2022-01-05 05:52:00
【问题描述】:
方法是:
public static boolean search(int matrix[][], int key)
如果在矩阵中找到键,该方法将返回 true, 矩阵大小将始终为 n x n ,矩阵是循环排序的,这里是 4x4 的示例 example
运行时效率必须低于 O(n^2) ,目标为 O(n log n)
这是我试图暗示的结构,代码不起作用只是我的思考过程
public static boolean search1 (int [][] matrix, int num)
{
int ROWS = matrix.length;
int COLS = matrix[0].length;
int end = COLS * ROWS;
int first_quarter_pivot = matrix[0][0];
int second_quarter_pivot = matrix[0][i];
int third_quarter_pivot = matrix[i][0];
int fourth_quarter_pivot = matrix[i][i];
if(num < first_quarter_pivot)
// if num smaller than first pivot binary search first quarter,if not found return false
binarySearch( );
if(num < second_quarter_pivot)
binarySearch( );
// if num smaller than second pivot binary search second quarter ,if not found return false
if(num < third_quarter_pivot )
binarySearch( );
// if num smaller than third pivot binary search third quarter ,if not found return false
// else search fourth quarter
else
binarySearch( );
// return false if nothing found
}
我可以t find the pivots to split the matrix to 4 quarters,I want to find the edges of each quarter,and I want to find in each quarter the bottom left corner which il 比较每个季度的 num 和
希望我提前说清楚了。
【问题讨论】:
标签: java arrays performance