-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path240_Search2DMatrixII.py
59 lines (51 loc) · 1.57 KB
/
240_Search2DMatrixII.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: [email protected]
class Solution(object):
"""
O(m+n)
Check the top-right corner.
If it's not the target, then remove the top row or rightmost column.
"""
def searchMatrix(self, matrix, target):
if not matrix or len(matrix[0]) < 1:
return False
m, n = len(matrix), len(matrix[0])
# We start search the matrix from top right corner
# Initialize the current position to top right corner.
row, col = 0, n - 1
while row < m and col >= 0:
if matrix[row][col] == target:
return True
elif matrix[row][col] > target:
col -= 1
else:
row += 1
return False
class Solution_2(object):
# O(m+n): same as the pre solution, more efficient and pythonic.
# According to
# https://leetcode.com/discuss/47571/4-lines-c-6-lines-ruby-7-lines-python-1-liners
def searchMatrix(self, matrix, target):
if not matrix or len(matrix[0]) < 1:
return False
n = len(matrix[0])
col = -1
for row in matrix:
while col + n > 0 and row[col] > target:
col -= 1
if row[col] == target:
return True
return False
class Solution_3(object):
# O(mn): 1 lines python. Just for fun
def searchMatrix(self, matrix, target):
return any(target in row for row in matrix)
"""
[[]]
0
[[-5]]
-2
[[1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24]]
12
"""