Skip to content

Commit 1042b64

Browse files
committed
leetcode
1 parent 564bcae commit 1042b64

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
3+
4+
5+
-* 74. Search a 2D Matrix *-
6+
7+
You are given an m x n integer matrix matrix with the following two properties:
8+
9+
Each row is sorted in non-decreasing order.
10+
The first integer of each row is greater than the last integer of the previous row.
11+
Given an integer target, return true if target is in matrix or false otherwise.
12+
13+
You must write a solution in O(log(m * n)) time complexity.
14+
15+
16+
17+
Example 1:
18+
19+
20+
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3
21+
Output: true
22+
Example 2:
23+
24+
25+
Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 13
26+
Output: false
27+
28+
29+
Constraints:
30+
31+
m == matrix.length
32+
n == matrix[i].length
33+
1 <= m, n <= 100
34+
-104 <= matrix[i][j], target <= 104
35+
36+
37+
*/
38+
39+
class A {
40+
bool searchMatrix(List<List<int>> matrix, int target) {
41+
final List<int> innerList =
42+
matrix.expand((innerArray) => innerArray).toList();
43+
return innerList.contains(target);
44+
}
45+
}
46+
47+
class Solution {
48+
int search(List<int> v, int target) {
49+
final int index = lowerBound(v, target);
50+
return index;
51+
}
52+
53+
bool searchMatrix(List<List<int>> matrix, int target) {
54+
final int n = matrix.length;
55+
final int m = matrix[0].length;
56+
57+
if (n == 1) {
58+
final int j = search(matrix[0], target);
59+
if (j == m) return false;
60+
return matrix[0][j] == target;
61+
}
62+
63+
final List<int> col0 = List<int>.generate(n, (i) => matrix[i][0]);
64+
65+
final int i0 = search(col0, target);
66+
67+
if (i0 == n) {
68+
final int j = search(matrix[i0 - 1], target);
69+
if (j == m) return false;
70+
return matrix[i0 - 1][j] == target;
71+
} else if (col0[i0] == target) {
72+
return true;
73+
} else if (i0 == 0) {
74+
return false;
75+
} else {
76+
final int j = search(matrix[i0 - 1], target);
77+
if (j == m) return false;
78+
return matrix[i0 - 1][j] == target;
79+
}
80+
}
81+
82+
int lowerBound(List<int> array, int target) {
83+
int left = 0;
84+
int right = array.length;
85+
while (left < right) {
86+
int mid = left + (right - left) ~/ 2;
87+
if (array[mid] < target) {
88+
left = mid + 1;
89+
} else {
90+
right = mid;
91+
}
92+
}
93+
return left;
94+
}
95+
}

SearchA2DMatrix/search_a_2d_matrix.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
func search(v []int, target int) int {
4+
index := lowerBound(v, target)
5+
return index
6+
}
7+
8+
func searchMatrix(matrix [][]int, target int) bool {
9+
n := len(matrix)
10+
m := len(matrix[0])
11+
12+
if n == 1 {
13+
j := search(matrix[0], target)
14+
if j == m {
15+
return false
16+
}
17+
return matrix[0][j] == target
18+
}
19+
20+
col0 := make([]int, n)
21+
for i := 0; i < n; i++ {
22+
col0[i] = matrix[i][0]
23+
}
24+
25+
i0 := search(col0, target)
26+
27+
if i0 == n {
28+
j := search(matrix[i0-1], target)
29+
if j == m {
30+
return false
31+
}
32+
return matrix[i0-1][j] == target
33+
} else if col0[i0] == target {
34+
return true
35+
} else if i0 == 0 {
36+
return false
37+
} else {
38+
j := search(matrix[i0-1], target)
39+
if j == m {
40+
return false
41+
}
42+
return matrix[i0-1][j] == target
43+
}
44+
}
45+
46+
func lowerBound(array []int, target int) int {
47+
left := 0
48+
right := len(array)
49+
for left < right {
50+
mid := left + (right-left)/2
51+
if array[mid] < target {
52+
left = mid + 1
53+
} else {
54+
right = mid
55+
}
56+
}
57+
return left
58+
}

SearchA2DMatrix/search_a_2d_matrix.md

Whitespace-only changes.

0 commit comments

Comments
 (0)