Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e49c989

Browse files
author
hj.tian
committed
feat: add LeetCode75 day23
add LeetCode75 day23
1 parent de0b5e6 commit e49c989

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

‎leetcode75/day23_33.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def search(self, nums: List[int], target: int) -> int:
6+
"""
7+
[ )
8+
4, 5, 6, 7, 0, 1, 2
9+
|
10+
左边有序,右边有可能有序,即至少有一边是有序的 1234567, 7012345
11+
0. target == nums[mid]:
12+
return mid
13+
1. 左边有序 nums[0] < nums[mid]
14+
target 是不是在左边?
15+
是: [left, mid) # nums[left] <= target < nums[mid]
16+
否: [mid+1, right)
17+
2. 右边有序
18+
target 是不是在右边?
19+
是: [mid+1, right) # nums[mid] < target <= nums[right-1]
20+
否: [left, mid)
21+
"""
22+
left, right = 0, len(nums)
23+
# [)
24+
while left < right:
25+
mid = left + (right - left) // 2
26+
if target == nums[mid]:
27+
return mid
28+
elif nums[0] < nums[mid]:
29+
if nums[left] <= target < nums[mid]:
30+
right = mid
31+
else:
32+
left = mid + 1
33+
else:
34+
if nums[mid] < target <= nums[right - 1]:
35+
left = mid + 1
36+
else:
37+
right = mid
38+
return -1
39+
40+
41+
if __name__ == "__main__":
42+
assert Solution().search(nums=[4, 5, 6, 7, 0, 1, 2], target=0) == 4
43+
assert Solution().search(nums=[4, 5, 6, 7, 0, 1, 2], target=3) == -1
44+
assert Solution().search(nums=[4, 5, 6, 7, 0, 1, 2], target=4) == 0
45+
assert Solution().search(nums=[4, 5, 6, 7, 0, 1, 2], target=2) == 6
46+
assert Solution().search(nums=[4, 5, 6, 7, 0, 1, 2], target=7) == 3
47+
assert Solution().search(nums=[1], target=0) == -1

‎leetcode75/day23_74.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
6+
if len(matrix) == 0 or len(matrix[0]) == 0:
7+
raise KeyError
8+
rows, cols = len(matrix), len(matrix[0])
9+
row, col = 0, cols - 1
10+
while row < rows and col >= 0:
11+
if matrix[row][col] == target:
12+
return True
13+
elif matrix[row][col] > target:
14+
col -= 1
15+
else:
16+
row += 1
17+
return False
18+
19+
20+
if __name__ == "__main__":
21+
assert (
22+
Solution().searchMatrix(
23+
matrix=[
24+
[1, 3, 5, 7],
25+
[10, 11, 16, 20],
26+
[23, 30, 34, 60],
27+
],
28+
target=3,
29+
)
30+
is True
31+
)
32+
assert (
33+
Solution().searchMatrix(
34+
matrix=[
35+
[1, 3, 5, 7],
36+
[10, 11, 16, 20],
37+
[23, 30, 34, 60],
38+
],
39+
target=13,
40+
)
41+
is False
42+
)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /