Skip to content

Commit 22a8a02

Browse files
committed
Update solution for problem 74
1 parent 3284c13 commit 22a8a02

File tree

3 files changed

+40
-38
lines changed

3 files changed

+40
-38
lines changed

LeetCode.Test/0051-0100/074-SearchA2DMatrix-Test.cs

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ public class _074_SearchA2DMatrix_Test
88
[TestMethod]
99
public void SearchMatrixTest_Exist_FirstRow()
1010
{
11-
var input = new int[,]
11+
var input = new int[][]
1212
{
13-
{ 1, 3, 5, 7 },
14-
{ 10, 11, 16, 20 },
15-
{ 23, 30, 34, 50 }
13+
new int[] { 1, 3, 5, 7 },
14+
new int[] { 10, 11, 16, 20 },
15+
new int[] { 23, 30, 34, 50 }
1616
};
1717

1818
var solution = new _074_SearchA2DMatrix();
@@ -24,11 +24,11 @@ public void SearchMatrixTest_Exist_FirstRow()
2424
[TestMethod]
2525
public void SearchMatrixTest_Exist_FirstColumn()
2626
{
27-
var input = new int[,]
27+
var input = new int[][]
2828
{
29-
{ 1, 3, 5, 7 },
30-
{ 10, 11, 16, 20 },
31-
{ 23, 30, 34, 50 }
29+
new int[] { 1, 3, 5, 7 },
30+
new int[] { 10, 11, 16, 20 },
31+
new int[] { 23, 30, 34, 50 }
3232
};
3333

3434
var solution = new _074_SearchA2DMatrix();
@@ -40,11 +40,11 @@ public void SearchMatrixTest_Exist_FirstColumn()
4040
[TestMethod]
4141
public void SearchMatrixTest_Exist_MidRow()
4242
{
43-
var input = new int[,]
43+
var input = new int[][]
4444
{
45-
{ 1, 3, 5, 7 },
46-
{ 10, 11, 16, 20 },
47-
{ 23, 30, 34, 50 }
45+
new int[] { 1, 3, 5, 7 },
46+
new int[] { 10, 11, 16, 20 },
47+
new int[] { 23, 30, 34, 50 }
4848
};
4949

5050
var solution = new _074_SearchA2DMatrix();
@@ -56,11 +56,11 @@ public void SearchMatrixTest_Exist_MidRow()
5656
[TestMethod]
5757
public void SearchMatrixTest_Exist_LastRow()
5858
{
59-
var input = new int[,]
59+
var input = new int[][]
6060
{
61-
{ 1, 3, 5, 7 },
62-
{ 10, 11, 16, 20 },
63-
{ 23, 30, 34, 50 }
61+
new int[] { 1, 3, 5, 7 },
62+
new int[] { 10, 11, 16, 20 },
63+
new int[] { 23, 30, 34, 50 }
6464
};
6565

6666
var solution = new _074_SearchA2DMatrix();
@@ -72,11 +72,11 @@ public void SearchMatrixTest_Exist_LastRow()
7272
[TestMethod]
7373
public void SearchMatrixTest_NotExist_LessThanFirst()
7474
{
75-
var input = new int[,]
75+
var input = new int[][]
7676
{
77-
{ 1, 3, 5, 7 },
78-
{ 10, 11, 16, 20 },
79-
{ 23, 30, 34, 50 }
77+
new int[] { 1, 3, 5, 7 },
78+
new int[] { 10, 11, 16, 20 },
79+
new int[] { 23, 30, 34, 50 }
8080
};
8181

8282
var solution = new _074_SearchA2DMatrix();
@@ -88,11 +88,11 @@ public void SearchMatrixTest_NotExist_LessThanFirst()
8888
[TestMethod]
8989
public void SearchMatrixTest_NotExist_GreaterThanLast()
9090
{
91-
var input = new int[,]
91+
var input = new int[][]
9292
{
93-
{ 1, 3, 5, 7 },
94-
{ 10, 11, 16, 20 },
95-
{ 23, 30, 34, 50 }
93+
new int[] { 1, 3, 5, 7 },
94+
new int[] { 10, 11, 16, 20 },
95+
new int[] { 23, 30, 34, 50 }
9696
};
9797

9898
var solution = new _074_SearchA2DMatrix();
@@ -104,11 +104,11 @@ public void SearchMatrixTest_NotExist_GreaterThanLast()
104104
[TestMethod]
105105
public void SearchMatrixTest_NotExist_GreaterThanFirstRow()
106106
{
107-
var input = new int[,]
107+
var input = new int[][]
108108
{
109-
{ 1, 3, 5, 7 },
110-
{ 10, 11, 16, 20 },
111-
{ 23, 30, 34, 50 }
109+
new int[] { 1, 3, 5, 7 },
110+
new int[] { 10, 11, 16, 20 },
111+
new int[] { 23, 30, 34, 50 }
112112
};
113113

114114
var solution = new _074_SearchA2DMatrix();

LeetCode/0051-0100/074-SearchA2DMatrix.cs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
//-----------------------------------------------------------------------------
2-
// Runtime: 164ms
3-
// Memory Usage:
4-
// Link:
2+
// Runtime: 92ms
3+
// Memory Usage: 25.5 MB
4+
// Link: https://leetcode.com/submissions/detail/409549251/
55
//-----------------------------------------------------------------------------
66

77
namespace LeetCode
88
{
99
public class _074_SearchA2DMatrix
1010
{
11-
public bool SearchMatrix(int[,] matrix, int target)
11+
public bool SearchMatrix(int[][] matrix, int target)
1212
{
13-
int columnLength = matrix.GetLength(1);
14-
int lo = 0, hi = matrix.Length - 1, mid = 0;
13+
if (matrix.Length == 0) return false;
14+
15+
int columnLength = matrix[0].Length;
16+
int lo = 0, hi = matrix.Length * columnLength - 1;
1517
int i, j;
1618
while (lo <= hi)
1719
{
18-
mid = lo + (hi - lo) / 2;
20+
var mid = lo + (hi - lo) / 2;
1921
i = mid / columnLength;
2022
j = mid % columnLength;
21-
if (matrix[i, j] < target) { lo = mid + 1; }
22-
else if (matrix[i, j] > target) { hi = mid - 1; }
23+
if (matrix[i][j] < target) { lo = mid + 1; }
24+
else if (matrix[i][j] > target) { hi = mid - 1; }
2325
else { return true; }
2426
}
2527
return false;

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The C# solutions for LeetCode problems.
123123
| 71 | Simplify Path | [C#](./LeetCode/0051-0100/071-SimplifyPath.cs)(132ms) | O(N) | O(N) | |
124124
| 72 | Edit Distance | [C#](./LeetCode/0051-0100/072-EditDistance.cs)(76ms) | O(N*M) | O(Min(N,M)) | |
125125
| 73 | Set Matrix Zeroes | [C#](./LeetCode/0051-0100/073-SetMatrixZeroes.cs)(184ms) | O(N*M) | O(N+M) | When use constant space, solution will slower |
126-
| 74 | Search a 2D Matrix | [C#](./LeetCode/0051-0100/074-SearchA2DMatrix.cs)(164ms) | O(Log(N+M)) | O(1) | |
126+
| 74 | Search a 2D Matrix | [C#](./LeetCode/0051-0100/074-SearchA2DMatrix.cs)(92ms) | O(Log(N+M)) | O(1) | |
127127
| 75 | Sort Colors | [C#](./LeetCode/0051-0100/075-SortColors.cs)(236ms) | O(N) | O(1) | |
128128
| 76 | Minimum Window Substring | [C#](./LeetCode/0051-0100/076-MinimumWindowSubstring.cs)(84ms) | O(N+M) | O(1) | |
129129
| 77 | Combinations | [C#](./LeetCode/0051-0100/077-Combinations.cs)(416ms) | O((N-K)!) | O(N!/K!) | |

0 commit comments

Comments
 (0)