【LeetCode】Search a 2D Matrix && 【九度】题目1384:二维数组中的查找
2014-01-22 10:12
633 查看
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
九度题目1384:二维数组中的查找
这个题目应该有些漏洞,其实是应该将要查找的数据,放在矩阵输入之后的。否则在输入的时候就可以查找,不需要任何算法或者技巧。
如以下Java AC代码:
将二维的转换成一维,利用二分查找很快就会求出结果。
思路仅供参考。
Java AC
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 ****************************************************************/
相关文章推荐
- 二维数组中的查找(LeetCode: 74. Search a 2D Matrix)
- Leetcode刷题记—— Search a 2D Matrix(在二维数组里查找)
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
- 剑指offer 面试题3:二维数组(矩阵)中数的查找(leetcode 74. Search a 2D Matrix) 题解
- [算法][LeetCode]Search a 2D Matrix——二维数组的二分查找
- 【leetcode】第74题 Search a 2D Matrix 题目+解析+JAVA代码
- leetcode解题之 Search a 2D Matrix java 版(在二维矩阵中查找)
- Search a 2D Matrix 分类: Leetcode(查找) 2015-04-11 10:19 24人阅读 评论(0) 收藏
- leetcode——Search a 2D Matrix 二维有序数组查找(AC)
- LeetCode OJ:Search a 2D Matrix(二维数组查找)
- Search a 2D Matrix:二分查找二维数组
- 【LeetCode】Search a 2D Matrix & Set Matrix Zeroes & Spiral Matrix & Spiral Matrix II
- LeetCode 74. Search a 2D Matrix(二维矩阵查找)
- leetcode——Search a 2D Matrix 二维有序数组查找(AC)
- Leetcode 240 Search a 2D Matrix II (二分法和分治法解决有序二维数组查找)
- Leetcode 74. Search a 2D Matrix 2D矩阵查找 解题报告
- 九度_题目1384:二维数组中的查找
- Search a 2D Matrix(在二维数组中查找)
- 【LeetCode】Search a 2D Matrix (杨氏矩阵查找)
- LeetCode-74-Search a 2D Matrix(二分查找)-Medium