일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | |
7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 15 | 16 | 17 | 18 | 19 | 20 |
21 | 22 | 23 | 24 | 25 | 26 | 27 |
28 | 29 | 30 | 31 |
Tags
- Regular Expression
- 109. Convert Sorted List to Binary Search Tree
- LeetCode
- iterator
- Class
- t1
- 밴픽
- data science
- 30. Substring with Concatenation of All Words
- 시바견
- 파이썬
- Python
- concurrency
- 315. Count of Smaller Numbers After Self
- 운영체제
- attribute
- 컴퓨터의 구조
- 프로그래머스
- Decorator
- Python Implementation
- Generator
- Substring with Concatenation of All Words
- Convert Sorted List to Binary Search Tree
- DWG
- Python Code
- kaggle
- Protocol
- shiba
- 43. Multiply Strings
- 715. Range Module
Archives
- Today
- Total
Scribbling
LeetCode: 240. Search a 2D Matrix II 본문
Several different solutions
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
j = bisect_right(matrix[0], target) - 1
i = bisect_right([mat[0] for mat in matrix], target) - 1
while i >= 0 and j >= 0:
if matrix[i][j] < target:
return False
for k in range(0, i+1):
if matrix[k][j] == target:
return True
for k in range(0, j+1):
if matrix[i][k] == target:
return True
i -= 1
j -= 1
O(NlogM)
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
for mat in matrix:
if mat[0] <= target <= mat[-1]:
l, r = 0, len(mat) - 1
while l <= r:
mid = (l+r) // 2
if mat[mid] == target:
return True
if mat[mid] < target:
l = mid + 1
else:
r = mid - 1
return False
O(N+M)
class Solution:
def searchMatrix(self, matrix: List[List[int]], target: int) -> bool:
y, x = len(matrix) - 1, 0
while y >= 0 and x <= len(matrix[0]) - 1:
if matrix[y][x] == target:
return True
if matrix[y][x] > target:
y -= 1
else:
x += 1
return False
'Computer Science > Coding Test' 카테고리의 다른 글
LeetCode: 287. Find the Duplicate Number (0) | 2021.12.28 |
---|---|
LeetCode: 279. Perfect Squares (0) | 2021.12.27 |
LeetCode: 283. Move Zeroes (0) | 2021.12.25 |
LeetCode: 234. Palindrome Linked List (0) | 2021.12.24 |
LeetCode: 239. Sliding Window Maximum (0) | 2021.12.23 |