74:搜索二维矩阵
This commit is contained in:
parent
a4331b798a
commit
097c487979
96
src/main/java/leetcode/editor/cn/SearchA2dMatrix.java
Normal file
96
src/main/java/leetcode/editor/cn/SearchA2dMatrix.java
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
//编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 每行中的整数从左到右按升序排列。
|
||||||
|
// 每行的第一个整数大于前一行的最后一个整数。
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 1:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
|
||||||
|
//输出:true
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 示例 2:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//输入:matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
|
||||||
|
//输出:false
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// 提示:
|
||||||
|
//
|
||||||
|
//
|
||||||
|
// m == matrix.length
|
||||||
|
// n == matrix[i].length
|
||||||
|
// 1 <= m, n <= 100
|
||||||
|
// -104 <= matrix[i][j], target <= 104
|
||||||
|
//
|
||||||
|
// Related Topics 数组 二分查找 矩阵
|
||||||
|
// 👍 478 👎 0
|
||||||
|
|
||||||
|
package leetcode.editor.cn;
|
||||||
|
|
||||||
|
import com.code.leet.entiy.TwoArray;
|
||||||
|
|
||||||
|
//74:搜索二维矩阵
|
||||||
|
class SearchA2dMatrix {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
//测试代码
|
||||||
|
Solution solution = new SearchA2dMatrix().new Solution();
|
||||||
|
//true
|
||||||
|
// TwoArray twoArray = new TwoArray("[[1,3,5,7],[10,11,16,20],[23,30,34,60]]",true);
|
||||||
|
// System.out.println(solution.searchMatrix(twoArray.getArr(), 3));
|
||||||
|
//true
|
||||||
|
TwoArray twoArray = new TwoArray("[[1,3]]", true);
|
||||||
|
System.out.println(solution.searchMatrix(twoArray.getArr(), 3));
|
||||||
|
}
|
||||||
|
|
||||||
|
//力扣代码
|
||||||
|
//leetcode submit region begin(Prohibit modification and deletion)
|
||||||
|
class Solution {
|
||||||
|
public boolean searchMatrix(int[][] matrix, int target) {
|
||||||
|
if (target < matrix[0][0] || target > matrix[matrix.length - 1][matrix[0].length - 1]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
int start = 0;
|
||||||
|
int end = matrix.length - 1;
|
||||||
|
while (start < end - 1) {
|
||||||
|
int mid = (start + end) / 2;
|
||||||
|
if (matrix[mid][0] == target) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (matrix[mid][0] > target) {
|
||||||
|
end = mid - 1;
|
||||||
|
} else {
|
||||||
|
start = mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (matrix[end][0] == target) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
int xIndex = matrix[end][0] > target ? start : end;
|
||||||
|
start = 0;
|
||||||
|
end = matrix[0].length - 1;
|
||||||
|
while (start < end) {
|
||||||
|
int mid = (start + end) / 2;
|
||||||
|
if (matrix[xIndex][mid] == target) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (matrix[xIndex][mid] < target) {
|
||||||
|
start = mid + 1;
|
||||||
|
} else {
|
||||||
|
end = mid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return matrix[xIndex][start] == target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//leetcode submit region end(Prohibit modification and deletion)
|
||||||
|
|
||||||
|
}
|
34
src/main/java/leetcode/editor/cn/SearchA2dMatrix.md
Normal file
34
src/main/java/leetcode/editor/cn/SearchA2dMatrix.md
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<p>编写一个高效的算法来判断 <code>m x n</code> 矩阵中,是否存在一个目标值。该矩阵具有如下特性:</p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li>每行中的整数从左到右按升序排列。</li>
|
||||||
|
<li>每行的第一个整数大于前一行的最后一个整数。</li>
|
||||||
|
</ul>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>示例 1:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/05/mat.jpg" style="width: 322px; height: 242px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
|
||||||
|
<strong>输出:</strong>true
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><strong>示例 2:</strong></p>
|
||||||
|
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/11/25/mat2.jpg" style="width: 322px; height: 242px;" />
|
||||||
|
<pre>
|
||||||
|
<strong>输入:</strong>matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
|
||||||
|
<strong>输出:</strong>false
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p> </p>
|
||||||
|
|
||||||
|
<p><strong>提示:</strong></p>
|
||||||
|
|
||||||
|
<ul>
|
||||||
|
<li><code>m == matrix.length</code></li>
|
||||||
|
<li><code>n == matrix[i].length</code></li>
|
||||||
|
<li><code>1 <= m, n <= 100</code></li>
|
||||||
|
<li><code>-10<sup>4</sup> <= matrix[i][j], target <= 10<sup>4</sup></code></li>
|
||||||
|
</ul>
|
||||||
|
<div><div>Related Topics</div><div><li>数组</li><li>二分查找</li><li>矩阵</li></div></div>\n<div><li>👍 478</li><li>👎 0</li></div>
|
Loading…
Reference in New Issue
Block a user