Repository navigation
Expand file tree
/
Copy pathSearcha2DMatrixII.py
More file actions
117 lines (106 loc) · 2.84 KB
/
Copy pathSearcha2DMatrixII.py
File metadata and controls
117 lines (106 loc) · 2.84 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2017/10/19
# @Author : wangdechang
class Solution:
"""
@param: matrix: A list of lists of integers
@param: target: An integer you want to search in matrix
@return: An integer indicate the total occurrence of target in the given matrix
"""
def searchMatrix(self, matrix, target):
# write your code here
size = len(matrix)
if size == 0:
return 0
rows = size
columns = len(matrix[0])
count = 0
# for i in range(columns):
# if matrix[0][i] == target:
# count += 1
# if i == 0:
# columns = 0
# else:
# columns = i - 1
# if matrix[0][i] > target:
# if i == 0:
# columns = 0
# else:
# columns = i - 1
# for i in range(size):
# if matrix[i][0] == target:
# count += 1
# if i == 0:
# rows = 0
# else:
# columns = i - 1
# if matrix[0][i] > target:
# if i == 0:
# rows = 0
# else:
# rows = i - 1
for row in range(size):
if row <= rows:
for column in range(columns):
if matrix[row][column] == target:
count += 1
columns = column
break
elif matrix[row][column] > target:
columns = column
break
else:
break
return count
matrix = [
[1,3,5,7],
[3,11,16,20],
[23,30,34,50]
]
target = 3
solution = Solution()
res = solution.searchMatrix(matrix,3)
print(res)
# size = len(matrix)
# rows = size
# columns = len(matrix[0])
# count = 0
#
# # for i in range(columns):
# # if matrix[0][i] == target:
# # count += 1
# # if i == 0:
# # columns = 0
# # else:
# # columns = i - 1
# # if matrix[0][i] > target:
# # if i == 0:
# # columns = 0
# # else:
# # columns = i - 1
# #
# # for i in range(size):
# # if matrix[i][0] == target:
# # count += 1
# # if i == 0:
# # rows = 0
# # else:
# # columns = i - 1
# # if matrix[0][i] > target:
# # if i == 0:
# # rows = 0
# # else:
# # rows = i - 1
#
# for row in range(size):
# if row <= rows:
# for column in range(columns):
# if matrix[row][column] == target:
# count += 1
# columns = column - 1
# break
# else:
# break
#
# print(count)