Wins the offer | 4-dimensional array to find

Brush wins the offer title

1 Introduction

To prepare for the interview algorithm, began to brush the question, to go from offer to prove safety. Plan within 10 days after brushing! Come on! (Headline autumn recruit Tiqian Pi before the interview)

2 title

(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.

3 ideas and answers

1 idea: search violence; pay attention to ways to take the shape of two-dimensional list.

Code:

# -*- coding:utf-8 -*-
class Solution:
    # array 二维列表
    def Find(self, target, array):
        # write code here
        # 思路1:暴力搜索
        a = len(array) # 行
        b = len(array[0]) # 列
        for i in range(a):
            for j in range(b):
                if array[i][j] == target:
                    return True

Results:
Here Insert Picture Description
also optimize it?

Thinking 2: Since each row of data is sorted, it is possible to first determine whether the element between the ends of the value, if the loop is traversed, no longer traverse. Note Consider the boundary conditions: the array is empty

Code:

# -*- coding:utf-8 -*-
class Solution:
    # array 二维列表
    def Find(self, target, array):
        # write code here
        # 思路2:判断每行首尾的元素 如果在这个范围就遍历查找 不在就下一个
        a = len(array) # 行
        b = len(array[0]) # 列

        for i in range(a):
            if b == 0:
                return False
            elif array[i][0] <= target <= array[i][-1]:
                # 符合条件再去遍历j
                for j in range(b):
                    if array[i][j] == target:
                        return True
            else:
                pass

Result:
Here Insert Picture Description
But the running time increased. . . Note did not optimize! Can you try to consider from time complexity point of view? It ignores the point: not only is sideways in ascending order, and also vertically ascending order of! That it supposed to do?

Ideas 3: starting from the lower left corner of the element, if greater than this value, right movement, less than this value, moving up.
Note that the cursor + while setting the initial conditions!

# -*- coding:utf-8 -*-
class Solution:
    # array 二维列表
    def Find(self, target, array):
        # write code here
        # 思路3:利用行列均排序 从左下角开始找起
        a = len(array)-1 # 行
        b = len(array[0])-1 # 列
        # 定义初始游标值
        i = a
        j = 0
        while i>=0 and j <= b:
            if target > array[i][j]:
                j += 1
            elif target < array[i][j]:
                i -= 1
            else:
                return True
        return False

Results:
Here Insert Picture Description
For now, the idea of running time 3 is the fastest! It is the best!

Guess you like

Origin blog.csdn.net/qq_27782503/article/details/94553060