1 Title Description
(The length of each one-dimensional array of same), each row from left to right in order of ascending sort, to sort each column in a two-dimensional array in order of increasing from top to bottom. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.
2 violent problem-solving
2.1 Analysis
One by one through the array, it returns true if found, searched the entire array can not be found, it returns false.
2.2 Code
class Solution:
# array 二维列表
def Find(self, target, array):
for i in array:
for j in i:
if j == target:
return True
return False
2.3 Complexity
time complexity:
space complexity:
3 from the top right corner to start looking
3.1 Analysis
The conditions given, the array is incremented from left to right, top to bottom incremented;
that is the right-most element in each row for the row maximum value;
Analyzing:
This value is equal to target, return true;
this value is less than the target, excluded the row, down one line;
this value is greater than the target, the column is excluded, a left;
3.2 Code
class Solution:
# array 二维列表
def Find(self, target, array):
rows = len(array)
cols = len(array[0])
i = 0
j = cols - 1
while i<rows and j>=0:
if array[i][j] == target:
return True
elif array[i][j] < target:
i = i + 1
elif array[i][j] > target:
j = j - 1
return False
3.3 Complexity
Time complexity: O (rows + cols)
Complexity Space: O (1)