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. 打印一圈
- 从左到右打印一行(这是一定有的)
- 当有两行及以上时,存在从上到下打印
- 当有两行及以上并有两列及以上时,存在从右到左
- 当有两列并有三行及以上时,存在从下到上打打印
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]