• 力扣题目解答自我总结(反转类题目)


    力扣题目解答自我总结(反转类题目)

    一.反转字符串

    1.题目描述

    编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。

    不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。

    你可以假设数组中的所有字符都是 ASCII 码表中的可打印字符。

    示例 1:

    输入:["h","e","l","l","o"]
    输出:["o","l","l","e","h"]
    

    示例 2:

    输入:["H","a","n","n","a","h"]
    输出:["h","a","n","n","a","H"]
    

    2.解答

    class Solution:
        def reverseString(self, s: List[str]) -> None:
            """
            Do not return anything, modify s in-place instead.
            """
            st_num = 0
            e_num = len(s)-1
            while e_num >st_num:
                s[st_num],s[e_num] = s[e_num],s[st_num]
                st_num += 1
                e_num -= 1
    #难点就是在O(1) 下运行
    

    二.反转整数

    1.题目描述

    给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。

    示例 1:

    输入: 123
    输出: 321
    

    示例 2:

    输入: -123
    输出: -321
    

    示例 3:

    输入: 120
    输出: 21
    

    2.解答

    class Solution:
        def reverse(self, x: int) -> int:
            new_x = '-'
            x = str(x)
            x = x[::-1]
            while x[-1] == 0:  #删除最后一位的 0
                x = x[:-1]
            if x[-1] == '-':   #删除有括号的
                x = x[:-1]
                while x[-1] == 0:
                    x = x[:-1]
                new_x += x
                x = new_x
            if int(x) < -2**31 or int(x) >2**31-1:
                return 0
            else:
                return int(x)
    

    三.旋转图像

    1.题目描述

    给定一个 n × n 的二维矩阵表示一个图像。

    将图像顺时针旋转 90 度。

    说明:

    你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。

    示例 1:

    给定 matrix = 
    [
      [1,2,3],
      [4,5,6],
      [7,8,9]
    ],
    
    原地旋转输入矩阵,使其变为:
    [
      [7,4,1],
      [8,5,2],
      [9,6,3]
    ]
    

    示例 2:

    给定 matrix =
    [
      [ 5, 1, 9,11],
      [ 2, 4, 8,10],
      [13, 3, 6, 7],
      [15,14,12,16]
    ], 
    
    原地旋转输入矩阵,使其变为:
    [
      [15,13, 2, 5],
      [14, 3, 4, 1],
      [12, 6, 8, 9],
      [16, 7,10,11]
    ]
    

    2.解答

    class Solution:
        def rotate(self, matrix: List[List[int]]) -> None:
            """
            Do not return anything, modify matrix in-place instead.
            """
            import copy
            new_list = []
            time = 0
            conter = 0
            matrix.reverse()
            matrix_1 = copy.copy(matrix)
            print(matrix)
            if len(matrix) != 0:
                for a in range(len(matrix)):
                    matrix[a] = []
                while len(matrix) > len(matrix_1[0]):
                    matrix.pop()
                while len (matrix) < len(matrix_1[0]):
                    matrix.append([])
                for b in range(len(matrix_1[0])):
                    for c in range(len(matrix_1)):
                        if time != len(matrix_1):
                            time += 1
                            matrix[conter].append(matrix_1[c][b])
                        elif time == len(matrix_1):
                            time = 1
                            conter += 1
                            matrix[conter].append(matrix_1[c][b])
    #这是我的思路比较low,先水平翻转,再按照子列表的长度,把他变成有拥有子列表长度一样的空的列表,再里面填写值进去,写完了头有点昏,等下次有空再优化下代码
    

    四.反转字符串中的单词 Ⅲ

    1.题目描述

    给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。

    示例 1:

    输入: "Let's take LeetCode contest"
    输出: "s'teL ekat edoCteeL tsetnoc" 
    

    注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。

    2.解答

    class Solution:
        def reverseWords(self, s: str) -> str:
            s_1 = ''
            for i in s.split():
                s_1 += i[::-1]+' '
            s_1 = s_1[:-1]
            return s_1
        #这个写法比较LOW
    

    五.有效数独

    1.题目描述

    判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。

    1. 数字 1-9 在每一行只能出现一次。
    2. 数字 1-9 在每一列只能出现一次。
    3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。

    img

    上图是一个部分填充的有效的数独。

    数独部分空格内已填入了数字,空白格用 '.' 表示。

    示例 1:

    输入:
    [
      ["5","3",".",".","7",".",".",".","."],
      ["6",".",".","1","9","5",".",".","."],
      [".","9","8",".",".",".",".","6","."],
      ["8",".",".",".","6",".",".",".","3"],
      ["4",".",".","8",".","3",".",".","1"],
      ["7",".",".",".","2",".",".",".","6"],
      [".","6",".",".",".",".","2","8","."],
      [".",".",".","4","1","9",".",".","5"],
      [".",".",".",".","8",".",".","7","9"]
    ]
    输出: true
    

    示例 2:

    输入:
    [
      ["8","3",".",".","7",".",".",".","."],
      ["6",".",".","1","9","5",".",".","."],
      [".","9","8",".",".",".",".","6","."],
      ["8",".",".",".","6",".",".",".","3"],
      ["4",".",".","8",".","3",".",".","1"],
      ["7",".",".",".","2",".",".",".","6"],
      [".","6",".",".",".",".","2","8","."],
      [".",".",".","4","1","9",".",".","5"],
      [".",".",".",".","8",".",".","7","9"]
    ]
    输出: false
    解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。
         但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。
    

    说明:

    • 一个有效的数独(部分已被填充)不一定是可解的。
    • 只需要根据以上规则,验证已经填入的数字是否有效即可。
    • 给定数独序列只包含数字 1-9 和字符 '.'
    • 给定数独永远是 9x9 形式的。

    2.解答

    class Solution:
        def isValidSudoku(self, board: List[List[str]]) -> bool:
            import copy
            x = True
            s_1 = ''
            s_2 = ''
            new_list = copy.deepcopy(List)
            list_1 = copy.copy(List)
            for num_1 in range(9):
                for num_2 in range(9):
                    new_list[num_1][num_2] = List[num_2][num_1]
            new_list_1 =copy.copy(new_list)
            for a_1 in range(9):
                for a_2 in range(9):
                    s_1 += str(List[a_1][a_2])
                    list_1[a_1] = s_1
                    s_2 += str(new_list[a_1][a_2])
                    new_list_1[a_1] = s_2
                    if len(list_1[a_1]) == 9:
                        list_1[a_1] = list_1[a_1].replace('.','')
                        new_list_1[a_1] = new_list_1[a_1].replace('.', '')
                        s_1 = ''
                        s_2 = ''
                        num_1 = len(list_1[a_1])-len(set(list_1[a_1]))
                        num_2 = len(new_list_1[a_1])-len(set(new_list_1[a_1]))
                        if num_1 != 0 or num_2 != 0:
                            x = False
            return x
        
        #pycharm能运行,力扣里就不行,很闷逼大神路过留个言
        #下面是力扣里报错内容
        #Line 11: TypeError: Parameters to generic types must be types. Got 0.
    

    如果你有更加吊炸天的解题方法留言,让我这渣渣学学```

  • 相关阅读:
    PHP is_float()、 is_double()、is_real()函数
    自动驾驶关键技术分解和流程
    自动驾驶行业内时间表和技术解析
    自动驾驶架构与实现路径
    ADAS单目摄像头行驶区域环境光检测图片标注
    多目标检测整合算法
    道路场景语义分割算法
    TTC测距算法
    TSR交通标志检测与识别
    Mobileye_EyeQ4功能和性能
  • 原文地址:https://www.cnblogs.com/pythonywy/p/10878582.html
Copyright © 2020-2023  润新知