File tree Expand file tree Collapse file tree 3 files changed +53
-7
lines changed
src/main/java/com/diguage/algo/leetcode Expand file tree Collapse file tree 3 files changed +53
-7
lines changed Original file line number Diff line number Diff line change @@ -62,6 +62,15 @@ include::{sourcedir}/_0074_SearchA2DMatrix.java[tag=answer]
6262include::{sourcedir}/_0074_SearchA2DMatrix_2.java[tag=answer]
6363----
6464--
65+
66+ 三刷::
67+ +
68+ --
69+ [{java_src_attr}]
70+ ----
71+ include::{sourcedir}/_0074_SearchA2DMatrix_3.java[tag=answer]
72+ ----
73+ --
6574====
6675
6776
Original file line number Diff line number Diff line change @@ -7,16 +7,18 @@ public class _0074_SearchA2DMatrix_2 {
77 * @since 2025-11-18 22:23:27
88 */
99 public boolean searchMatrix (int [][] matrix , int target ) {
10- int m = matrix .length ;
11- int n = matrix [0 ].length ;
12- int low = 0 , high = m * n - 1 ;
10+ int row = matrix .length ;
11+ int column = matrix [0 ].length ;
12+ int low = 0 , high = row * column - 1 ;
1313 while (low <= high ) {
1414 int mid = low + (high - low ) / 2 ;
15- int row = mid / n ;
16- int col = mid % n ;
17- if (matrix [row ][col ] == target ) {
15+ int r = mid / column ;
16+ int c = mid % column ;
17+ int num = matrix [r ][c ];
18+ if (num == target ) {
1819 return true ;
19- } else if (matrix [row ][col ] < target ) {
20+ }
21+ if (num < target ) {
2022 low = mid + 1 ;
2123 } else {
2224 high = mid - 1 ;
Original file line number Diff line number Diff line change 1+ package com .diguage .algo .leetcode ;
2+
3+ public class _0074_SearchA2DMatrix_3 {
4+ // tag::answer[]
5+
6+ /**
7+ * @author D瓜哥 · https://www.diguage.com
8+ * @since 2025-11-18 22:23:27
9+ */
10+ public boolean searchMatrix (int [][] matrix , int target ) {
11+ int r = 0 , c = matrix [r ].length - 1 ;
12+ while (r < matrix .length && 0 <= c ) {
13+ int num = matrix [r ][c ];
14+ if (num == target ) {
15+ return true ;
16+ }
17+ if (target < num ) {
18+ c --;
19+ } else {
20+ r ++;
21+ }
22+ }
23+ return false ;
24+ }
25+ // end::answer[]
26+
27+ static void main () {
28+ new _0074_SearchA2DMatrix_3 ()
29+ .searchMatrix (new int [][]{
30+ {1 , 3 , 5 , 7 },
31+ {10 , 11 , 16 , 20 },
32+ {23 , 30 , 34 , 60 }},
33+ 3 );
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments