Second, look for two-dimensional array

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.
 
python:
 1 # -*- coding:utf-8 -*-
 2 class Solution:
 3     def Find(self, target, array):
 4         # write code here
 5         row = len(array) - 1
 6         col = len(array[0]) - 1
 7         r = row
 8         c = 0
 9         while r >= 0 and c<= col:
10             if target < array[r][c]:
11                 r -= 1
12             elif target > array[r][c]:
13                 c += 1
14             else:
15                 return True
16         return False
Ideas:

Start comparing the first element of the last row of the array.

Greater than this element, then just compare this line, then increasing column index, constant comparison, if not to false.

Less than the element, then start again from the line in the comparison, then the index is increasing, constant comparison, if not to false.

If the integer which, when determined to:

             elif target > array[r][c]:
                 c += 1

This step does not satisfy the conditions indicating the presence of a number equal to the integer greater than the number of the first occurrence of this must be equal to an integer number, so in the end a situation that is to find the integer and returns True.

Guess you like

Origin www.cnblogs.com/pacino12134/p/10938919.html