Skip to content

Commit 8e58c5c

Browse files
committed
update 74 34
1 parent 86f5872 commit 8e58c5c

6 files changed

+42
-33
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
4444
| [] | [blind75.md](./list/blind75.md) | 77/77 | - |
4545
| [] | [geekbang.md](./list/geekbang.md) | 55/55 | - |
4646
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
47-
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 4/128 | 2 vips |
47+
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 7/128 | 3 vips |
4848
| [🔲] | [9c-top.md](./list/9c-top.md) | 5/48 | - |
4949
| [🔲] | [leetcode-contest.md](./list/leetcode-contest.md) | 10/56 | - |
5050
| [🔲] | [leetcode-google.md](./list/leetcode-google.md) | 0/7 | - |

leetcode/34.find-first-and-last-position-of-element-in-sorted-array.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,17 @@ class Solution {
5454

5555
return start;
5656
}
57+
};
58+
59+
class Solution {
60+
public:
61+
vector<int> searchRange(vector<int>& nums, int target) {
62+
auto first = lower_bound(nums.begin(), nums.end(), target);
63+
if (first == nums.end() || *first != target) {
64+
return vector<int>{-1, -1};
65+
}
66+
67+
auto last = lower_bound(nums.begin(), nums.end(), target + 1);
68+
return vector<int>{int(first - nums.begin()), int(last - nums.begin() - 1)};
69+
}
5770
};

leetcode/74.search-a-2d-matrix.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,25 +37,27 @@
3737
class Solution {
3838
public:
3939
bool searchMatrix(vector<vector<int>>& matrix, int target) {
40-
int m = matrix.size(), n = matrix[0].size();
41-
int start = 0;
42-
int end = m * n;
40+
int m = matrix.size();
41+
int n = matrix[0].size();
42+
int left = 0;
43+
int right = m * n - 1;
4344

44-
while (start < end) {
45-
int mid = start + (end - start) / 2;
45+
while (left < right) {
46+
int mid = left + (right - left) / 2;
4647
int i = mid / n, j = mid % n;
4748

4849
if (matrix[i][j] == target) {
4950
return true;
5051
}
5152

5253
if (matrix[i][j] > target) {
53-
end = mid;
54+
right = mid;
5455
} else {
55-
start = mid + 1;
56+
left = mid + 1;
5657
}
5758
}
5859

59-
return false;
60+
int i = left / n, j = left % n;
61+
return matrix[i][j] == target;
6062
}
6163
};

leetcode/74.search-a-2d-matrix.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,28 +34,23 @@
3434
#
3535
#
3636

37-
class Solution(object):
38-
def searchMatrix(self, matrix, target):
39-
"""
40-
:type matrix: List[List[int]]
41-
:type target: int
42-
:rtype: bool
43-
"""
44-
m = len(matrix)
45-
n = len(matrix[0])
46-
start = 0
47-
end = m * n
48-
49-
while start < end:
50-
mid = start + (end - start) // 2
51-
i, j = mid // n, mid % n
37+
class Solution:
38+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
39+
n = len(matrix)
40+
m = len(matrix[0])
41+
left = 0
42+
right = n * m - 1
5243

44+
while left < right:
45+
mid = left + (right - left) // 2
46+
i, j = mid // m, mid % m
5347
if matrix[i][j] == target:
5448
return True
5549

56-
if matrix[i][j] > target:
57-
end = mid
50+
if matrix[i][j] < target:
51+
left = mid + 1
5852
else:
59-
start = mid + 1
53+
right = mid
6054

61-
return False
55+
i, j = left // m, left % m
56+
return matrix[i][j] == target

lintcode/462.total-occurrence-of-target.vip

Whitespace-only changes.

list/9c-basic.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,11 @@
1313
6. https://leetcode.com/problems/first-bad-version/
1414
7. https://www.lintcode.com/problem/search-in-a-big-sorted-array/
1515
8. https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/
16+
9. https://leetcode.com/problems/search-a-2d-matrix/
17+
10. https://leetcode.com/problems/search-a-2d-matrix-ii/
18+
11. https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/
19+
12. https://www.lintcode.com/problem/total-occurrence-of-target/
1620

17-
18-
- https://www.lintcode.com/problem/search-a---matrix/
19-
- https://www.lintcode.com/problem/search-a---matrix-ii/
20-
- https://www.lintcode.com/problem/search-for-a-range/
21-
- https://www.lintcode.com/problem/total-occurrence-of-target/
2221
- https://www.lintcode.com/problem/maximum-number-in-mountain-sequence/
2322
- https://www.lintcode.com/problem/smallest-rectangle-enclosing-black-pixels/
2423
- https://www.lintcode.com/problem/find-peak-element/

0 commit comments

Comments
 (0)