zoukankan      html  css  js  c++  java
  • 面试题3:二维数组中的查找

    代码如下:

     1 # coding:utf-8
     2 """
     3 =========================================
     4 File:SearchMatrix.py
     5 Author:Nobita
     6 Time:2019/12/18 20:46
     7 E-main:364942727@qq.com
     8 Description:面试题3:二堆数组中的查找
     9 Software:PyCharm
    10 =========================================
    11 """
    12 
    13 '''
    14 题目:
    15 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。
    16 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否有该整数。
    17 
    18 输入:
    19 matrix = [
    20   [1,   3,  5,  7],
    21   [10, 11, 16, 20],
    22   [23, 30, 34, 50]
    23 ]
    24 target_true = 3
    25 输出: True
    26 target_false = 13
    27 输出: False
    28 '''
    29 class Solution:
    30     def searchMarix(self, matrix, target):
    31         num_row = len(matrix)
    32         num_col = len(matrix[0])
    33         if num_row > 0 and num_col > 0:
    34             i, j = 0, num_col - 1
    35             while i < num_row and j >= 0:
    36                 if target == matrix[i][j]:
    37                     return True
    38                 elif target < matrix[i][j]:
    39                     j -= 1
    40                 else:
    41                     i += 1
    42         return False
    43 
    44 if __name__ == '__main__':
    45     matrix = [
    46         [1, 3, 5, 7],
    47         [10, 11, 16, 20],
    48         [23, 30, 34, 50]
    49     ]
    50     target_true = 3
    51     target_false = 13
    52     print(Solution().searchMarix(matrix, target_true))
    53     print(Solution().searchMarix(matrix, target_false))

    代码自测:

     1 # -*- coding: utf-8 -*-
     2 """
     3 @File:test_SearchMatrix.py    
     4 @E-mail:364942727@qq.com
     5 @Time:2019-12-19 8:24 
     6 @Author:Nobita   
     7 @Version:1.0   
     8 @Desciption:对SearchMatrix函数进行单元测试
     9 """
    10 
    11 import unittest
    12 from .SearchMatrix import Solution
    13 
    14 
    15 class TestSearchMatrix(unittest.TestCase):
    16     def setUp(self):
    17         global matrix
    18         matrix = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
    19 
    20     def test_01_MaxNum_in_Matrix(self):
    21         '''测试二维数组中的最大值,预期输出:True'''
    22         target = 50
    23         result = Solution().searchMarix(matrix, target)
    24         self.assertEqual(result, True, msg='test_01_测试失败!')
    25 
    26     def test_02_MinNum_in_Matrix(self):
    27         '''测试二维数组中的最小值,预期输出:True'''
    28         target = 1
    29         result = Solution().searchMarix(matrix, target)
    30         self.assertEqual(result, True, msg='test_02_测试失败!')
    31 
    32     def test_03_MidNum_in_Matrix(self):
    33         '''测试二维数组中的中间值,预期输出:True'''
    34         target = 11
    35         result = Solution().searchMarix(matrix, target)
    36         self.assertEqual(result, True, msg='test_03_测试失败!')
    37 
    38     def test_04_OverMaxNum_not_in_Matrix(self):
    39         '''测试二维数组中超过最大值的数,预期输出:False'''
    40         target = 51
    41         result = Solution().searchMarix(matrix, target)
    42         self.assertEqual(result, False, msg='test_04_测试失败!')
    43 
    44     def test_05_LessMinNum_not_in_Matrix(self):
    45         '''测试二维数组中小于最小值的数,预期输出:False'''
    46         target = 0
    47         result = Solution().searchMarix(matrix, target)
    48         self.assertEqual(result, False, msg='test_05_测试失败!')
    49 
    50     def test_06_MatrixIsNull(self):
    51         '''测试二维数组中空值矩阵,预期输出:False'''
    52         target = 1
    53         null_matrix = [[]]
    54         result = Solution().searchMarix(null_matrix, target)
    55         self.assertEqual(result, False, msg='test_06_测试失败!')
    56 
    57 
    58 if __name__ == '__main__':
    59     unittest.main()
  • 相关阅读:
    sys.stdout.flush-倒计时
    wget 网站扒取
    万能英数脚本
    sample function
    get_time
    读取指定行
    request设置cookies
    resize2fs
    闭包与认识装饰器
    函数的名称空间与作用域
  • 原文地址:https://www.cnblogs.com/chenshengkai/p/12072593.html
Copyright © 2011-2022 走看看