Search a 2D Matrix
Total Accepted: 4984 Total Submissions: 16715 My Submissions
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
Integers in each row are sorted from left to right.
The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[
[1, 3, 5, 7],
[10, 11, 16, 20],
[23, 30, 34, 50]
]
Given target = 3, return true.
和九度题目1384:二维数组中的查找完全一样,本质上应该都是二分查找。
将二维转换为一维,然后在一维数组中进行二分查找。
LeetCode Java AC
public class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
if(matrix == null){
return false;
}
int m = matrix.length;
int n = matrix[0].length;
int array[] = new int[m*n];
int k = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
array[k] = matrix[i][j];
k++;
}
}
boolean flag = findT(array , target , 0 ,m * n-1);
return flag;
}
public boolean findT(int[] array, int t, int low, int high) {
while (low <= high) {
int mid = (low + high) >> 1;
if (array[mid] > t) {
high = mid - 1;
}else if(array[mid] < t){
low = mid + 1;
}else {
return true;
}
}
return false;
}
}
九度题目1384:二维数组中的查找
这个题目应该有些漏洞,其实是应该将要查找的数据,放在矩阵输入之后的。否则在输入的时候就可以查找,不需要任何算法或者技巧。
如以下Java AC代码:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main{
/*
* 1384
*/
public static void main(String[] args) throws Exception{
StreamTokenizer st = new StreamTokenizer
(new BufferedReader(new InputStreamReader(System.in)));
while (st.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) st.nval;
st.nextToken();
int n = (int) st.nval;
st.nextToken();
int t = (int) st.nval;
int array[] = new int[m*n];
int k = 0;
boolean flag = false;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
st.nextToken();
array[k] = (int) st.nval;
if (array[k] == t) {
flag = true;
}
k++;
}
}
System.out.println(flag == true ? "Yes" : "No");
}
}
}
/**************************************************************
Problem: 1384
User: wangzhenqing
Language: Java
Result: Accepted
Time:1840 ms
Memory:31240 kb
****************************************************************/
我认为,题目的本质应该是去考察二分查找的。
将二维的转换成一维,利用二分查找很快就会求出结果。
思路仅供参考。
Java AC
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main{
/*
* 1384
*/
public static void main(String[] args) throws Exception{
StreamTokenizer st = new StreamTokenizer
(new BufferedReader(new InputStreamReader(System.in)));
while (st.nextToken() != StreamTokenizer.TT_EOF) {
int m = (int) st.nval;
st.nextToken();
int n = (int) st.nval;
st.nextToken();
int t = (int) st.nval;
int array[] = new int[m*n];
int k = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
st.nextToken();
array[k] = (int) st.nval;
k++;
}
}
boolean flag = findT(array , t , 0 ,m * n-1);
System.out.println(flag == true ? "Yes" : "No");
}
}
private static boolean findT(int[] array, int t, int low, int high) {
while (low <= high) {
int mid = (low + high) >> 1;
if (array[mid] > t) {
high = mid - 1;
}else if(array[mid] < t){
low = mid + 1;
}else {
return true;
}
}
return false;
}
}
/**************************************************************
Problem: 1384
User: wangzhenqing
Language: Java
Result: Accepted
Time:1890 ms
Memory:32700 kb
****************************************************************/