剑指offer的Python实现(四)

16.合并两个排序的链表(递归)

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

# -*- coding:utf-8 -*-

# class ListNode:

#     def __init__(self, x):

#         self.val = x

#         self.next = None

class Solution:

    # 返回合并后列表

    def Merge(self, pHead1, pHead2):

        # write code here

        if pHead1 == None:

            return pHead2

        if pHead2 == None:

            return pHead1

        ListNode1 = None

        if (pHead1.val < pHead2.val):

            ListNode1 = pHead1

            ListNode1.next = self.Merge(pHead1.next, pHead2)#用到递归时,需要加上self.

        else:

            ListNode1 = pHead2

            ListNode1.next = self.Merge(pHead1, pHead2.next)

        return ListNode1

 

17.树的子结构

题目描述

输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

 分为两步:1.在树A种找到和B的根结点的值一样的结点R

2.判断A中以R为根结点的子树是不是包含和B一样的结构

自己的错误代码

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

    def HasSubtree(self, pRoot1, pRoot2):

        # write code here

        result = None(False)

        if (pRoot1 != None &&(and) pRoot2 != None):

            if (pRoot1.val == pRoot2.val):

                result = 少了(self.)DoesTree1HaveTree2(pRoot1, pRoot2)

            if (!result)if not result:

                result = self.HasSubtree(pRoot1.left, pRoot2)

            if (!result) if not result:

                result = self.HasSubtree(pRoot2(1).right, pRoot2)

        return result

    def DoesTree1HaveTree2(self,pRoot1, pRoot2):

        if (pRoot2 == None):

            return true(True)

        if (pRoot1 == None):

            return false(False)

        if (pRoot1.val != pRoot2.val):

            return false(False)

        return self.DoesTree1HaveTree2(pRoot1.left, pRoot2.left)&&(and) self. DoesTree1HaveTree2(pRoot1.right, pRoot2.right)

正确代码:

# -*- coding:utf-8 -*-

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

    def HasSubtree(self, pRoot1, pRoot2):

        # write code here

        result = False

        if pRoot1 != None and pRoot2 != None:

            if pRoot1.val == pRoot2.val:

                result = self.DoesTree1haveTree2(pRoot1, pRoot2)

            if not result:

                result = self.HasSubtree(pRoot1.left, pRoot2)

            if not result:

                result = self.HasSubtree(pRoot1.right, pRoot2)

        return result

    # 用于递归判断树的每个节点是否相同

    # 需要注意的地方是: 前两个if语句不可以颠倒顺序

    # 如果颠倒顺序, 会先判断pRoot1是否为None, 其实这个时候pRoot2的结点已经遍历完成确定相等了, 但是返回了False, 判断错误

    def DoesTree1haveTree2(self, pRoot1, pRoot2):

        if pRoot2 == None:

            return True

        if pRoot1 == None:

            return False

        if pRoot1.val != pRoot2.val:

            return False

        return self.DoesTree1haveTree2(pRoot1.left, pRoot2.left) and self.DoesTree1haveTree2(pRoot1.right, pRoot2.right)

 

18.二叉树的镜像

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:

二叉树的镜像定义:源二叉树 
            8
           /  \
          6   10
         / \  / \
        5  7 9 11
        镜像二叉树
            8
           /  \
          10   6
         / \  / \
        11 9 7  5

自己的错误代码

# -*- coding:utf-8 -*-

# class TreeNode:

#     def __init__(self, x):

#         self.val = x

#         self.left = None

#         self.right = None

class Solution:

    # 返回镜像树的根节点

    def Mirror(self, root):

        # write code here

        if root == None or root.left == None and p.right(绿色这部分不要):

            return root

        nonlocal p

        p = None(这两行不要)

        p = root.left

        root.left = root.right

        root.right = p

       

        if root.left:

            self.Mirror(root.left)

        if root.right:

            self.Mirror(root.right)

 

 

正确代码:

class Solution:

    def Mirror(self, root):

        # write code here

        if not root:

            return root

        node=root.left

        root.left=root.right

        root.right=node

        self.Mirror(root.left)

        self.Mirror(root.right)

        return root

 

19. 顺时针打印矩阵

题目描述

输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下4 X 4矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

 

1.

# -*- coding:utf-8 -*-

class Solution:

    # matrix类型为二维列表,需要返回列表

    def printMatrix(self, matrix):

        # write code here

        res = []

        n = len(matrix)

        m = len(matrix[0])

        if m==1 and n==1:

            res = [matrix[0][0]]

            return res

        else:

            for o in range((min(m,n)+1)//2):

                [res.append(matrix[o][i]) for i in range(o,m-o)]

                [res.append(matrix[j][m-o-1]) for j in range(o,n-o) if matrix[j][m-o-1] not in res]

                [res.append(matrix[n-o-1][k]) for k in range(m-1-o,o-1,-1) if matrix[n-o-1][k] not in res]

                [res.append(matrix[l][o]) for l in range(n-1-o,o-1,-1) if matrix[l][o] not in res]

            return res

 

2. 链接:https://www.nowcoder.com/questionTerminal/9b4c81a02cd34f76be2659fa0d54342a
来源:牛客网

思路:

分为两个步骤:

1. 打印一圈

  1. 从左到右打印一行(这是一定有的)
  2. 当有两行及以上时,存在从上到下打印
  3. 当有两行及以上并有两列及以上时,存在从右到左
  4. 当有两列并有三行及以上时,存在从下到上打打印

2. 起点进入下一圈,即进入下一个循环

初始几点为(0, 0), 打印一圈后有(1, 1), 再打印一圈为(2, 2)

都存在行数 > 起点 * 2 并且列数 > 起点 * 2

简单来说就是, 如果把二维数组等分为四个部分 中点都为中心,那么起点肯定都位于左上部分。根据这个条件可以判断是否还有圈数

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

# -*- coding:utf-8 -*-

class Solution:

    # matrix类型为二维列表,需要返回列表

    def printMatrix(self, matrix):

        # write code here

        if not matrix:

            return []

        columns = len(matrix[0])

        rows = len(matrix)

         

        def printMatrixCircle(start):

            endRow = rows - 1 - start

            endColumn = columns - 1 - start

             

            # 从左到右打印一行

            for y in range(start, endColumn + 1):

                result.append(matrix[start][y])

                 

            # 从上到下打印一列

            if endRow > start:

                for x in range(start + 1, endRow + 1):

                    result.append(matrix[x][endColumn])

                     

            # 从右到左打印一行

            if endColumn > start and endRow > start:

                for y in range(endColumn - 1, start - 1, -1):

                    result.append(matrix[endRow][y])

             

            # 从下到上打印

            if endRow > start + 1 and endColumn > start:

                for x in range(endRow - 1, start, -1):

                    result.append(matrix[x][start])

         

        start = 0

        result = []

        while columns > start * 2 and rows > start * 2:

            printMatrixCircle(start)

            start += 1

         

         

        return result

 

 

20.包含min 函数的栈

题目描述

定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))

# -*- coding:utf-8 -*-

class Solution:

    def __init__(self):

        self.stack = []

        self.assist = []

    def push(self, node):

        # write code here

        min = self.min()

        if not min or node < min:

            self.assist.append(node)

        else:

            self.assist.append(min)

        self.stack.append(node)

       

    def pop(self):

        # write code here

        if self.stack:

            self.assist.pop()

            return self.stack.pop()

       

    def top(self):

        # write code here

        if self.stack:

            return self.stack[-1]

       

    def min(self):

        # write code here

        if self.assist:

            return self.assist[-1]

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
毕业设计,基于SpringBoot+Vue+MySQL开发的海滨体育馆管理系统,源码+数据库+毕业论文+视频演示 本基于Spring Boot的海滨体育馆管理系统设计目标是实现海滨体育馆的信息化管理,提高管理效率,使得海滨体育馆管理工作规范化、高效化。 本文重点阐述了海滨体育馆管理系统的开发过程,以实际运用为开发背景,基于Spring Boot框架,运用了Java技术和MySQL作为系统数据库进行开发,充分保证系统的安全性和稳定性。本系统界面良好,操作简单方便,通过系统概述、系统分析、系统设计、数据库设计、系统测试这几个部分,详细的说明了系统的开发过程,最后并对整个开发过程进行了总结,实现了海滨体育馆相关信息管理的重要功能。 本系统的使用使管理人员从繁重的工作中解脱出来,实现无纸化办公,能够有效的提高海滨体育馆管理效率。 关键词:海滨体育馆管理,Java技术,MySQL数据库,Spring Boot框架 本基于Spring Boot的海滨体育馆管理系统主要实现了管理员功能模块和学生功能模块两大部分,这两大功能模块分别实现的功能如下: (1)管理员功能模块 管理员登录后可对系统进行全面管理操作,包括个人中心、学生管理、器材管理、器材借出管理、器材归还管理、器材分类管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理、灯光保修管理、体育论坛以及系统管理。 (2)学生功能模块 学生在系统前台可查看系统信息,包括首页、器材、体育论坛以及体育资讯等,没有账号的学生可进行注册操作,注册登录后主要功能模块包括个人中心、器材管理、器材借出管理、器材归还管理、校队签到管理、进入登记管理、离开登记管理、活动预约管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值