剑指Offer-Python(16-20)
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
p1 = pHead1
p2 = pHead2
p = ListNode(0)
pNew = p
while p1 and p2:
if p1.val <= p2.val:
p.next = p1
p = p.next
p1 = p1.next
else:
p.next = p2
p = p.next
p2 = p2.next
if not p1:
p.next = p2
elif not p2:
p.next = p1
return pNew.next head1 = ListNode(1)
t1 = ListNode(3)
head1.next = t1
t2 = ListNode(5)
t1.next = t2
t2.next = None head2 = ListNode(2)
t1 = ListNode(4)
head2.next = t1
t2 = ListNode(6)
t1.next = t2
t2.next = None s = Solution()
print(s.Merge(head1, head2).next.val)
17、树的子结构
递归
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution:
def HasSubtree(self, root1, root2):
# write code here
if not root1 or not root1:
return False
return self.doesTreeHashTree2(root1, root2) or self.HasSubtree(root1.right, root2) or self.HasSubtree(
root1.left, root2) def doesTreeHashTree2(self, root1, root2):
if not root2:
return True
if not root1:
return False
if root1.val != root2.val:
return False
return self.doesTreeHashTree2(root1.left, root2.left) and self.doesTreeHashTree2(root1.right, root2.right) s = Solution()
tree1 = TreeNode(1)
t1 = TreeNode(2)
t2 = TreeNode(3)
t3 = TreeNode(4)
t4 = TreeNode(5)
tree1.left = t1
tree1.right = t2
t1.left = t3
t1.right = t4 tree2 = TreeNode(2)
t3 = TreeNode(4)
t4 = TreeNode(5)
tree2.left = t3
tree2.right = t4 print(s.HasSubtree(tree1, tree2))
18、二叉树的镜像
递归
# -*- 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 not root:
return
if not root.left and not root.right:
return
temp = root.left
root.left = root.right
root.right = temp
if root.left:
self.Mirror(root.left)
elif root.right:
self.Mirror(root.right) s = Solution()
tree1 = TreeNode(1)
t1 = TreeNode(2)
t2 = TreeNode(3)
t3 = TreeNode(4)
t4 = TreeNode(5)
tree1.left = t1
tree1.right = t2
t1.left = t3
t1.right = t4 s.Mirror(tree1)
print(tree1.right.right.val)
19、顺时针打印矩阵
# -*- coding:utf-8 -*-
class Solution:
# matrix类型为二维列表,需要返回列表
def printMatrix(self, matrix):
# write code here
row = len(matrix)
con = len(matrix[0])
cir = 0
t = []
while row > 2 * cir and con > 2 * cir:
for i in range(cir, con - cir): # 右
t.append(matrix[cir][i])
if cir < row - cir - 1: # 下
for i in range(cir + 1, row - cir):
t.append(matrix[i][con - cir - 1])
if con - cir - 1 > cir and row - cir - 1 > cir: # 左
for i in range(con - cir - 2, cir-1, -1):
t.append(matrix[row - cir - 1][i])
if cir < con - cir - 1 and cir < row - cir - 2: # 下
for i in range(row - cir - 2, cir, -1):
t.append(matrix[i][cir])
cir += 1
return t s = Solution()
a = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
t = s.printMatrix(a)
print(t)
20、包含min函数的栈
要求min函数时间复杂度为O(1),因此增加辅助栈存 “当前最小元素“
# -*- coding:utf-8 -*-
class Solution:
def __init__(self):
self.q = []
self.mi = [] def push(self, node):
min = self.min()
if not self.mi or node <= min:
self.mi.append(node)
else:
self.mi.append(min)
self.q.append(node) def pop(self):
if self.q:
self.mi.pop()
return self.q.pop() def top(self):
if self.q:
return self.q[-1] def min(self):
if self.mi:
return self.mi[-1] s = Solution()
s.push(1)
s.push(2)
s.push(5)
s.push(3)
print(s.min())
print(s.pop())
print(s.top())
剑指Offer-Python(16-20)的更多相关文章
- 剑指Offer:面试题20——顺时针打印矩阵(java实现)
题目描述: 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数 字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...
- 剑指offer——python【第54题】字符流中第一个不重复的字符
题目描述 请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- 剑指 offer面试题20 顺时针打印矩阵
[题目描述] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...
- 剑指offer计划16( 排序简单)---java
1.1.题目1 剑指 Offer 45. 把数组排成最小的数 1.2.解法 这题看的题解,发现自己思路错了. 这里直接拿大佬的题解来讲吧. 一开始这里就把创一个string的数组来存int数组 Str ...
- 剑指offer——python【第16题】合并两个有序链表
题目描述 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-& ...
- 剑指offer——python【第2题】替换空格
题目描述 请实现一个函数,将一个字符串中的每个空格替换成“%20”. 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy. 理解 很容易想到用pytho ...
- 【剑指offer】题目20 顺时针打印矩阵
输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出 ...
- 剑指offer——python【第23题】二叉搜索树的后序遍历序列
题目描述 输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果.如果是则输出Yes,否则输出No.假设输入的数组的任意两个数字都互不相同. 解题思路 首先要清楚,这道题不是让你去判断一个给定 ...
- 剑指offer——python【第38题】二叉树的深度
题目描述 输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. 解题思路 想了很久..首先本渣渣就不太理解递归在python中的实现 ...
- 剑指offer——python【第28题】数组 中出现次数超过一半的数字
题目描述 数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字.例如输入一个长度为9的数组{1,2,3,2,2,2,5,4,2}.由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2. ...
随机推荐
- xshell选项卡不见了
最近使用xshell的时候发现建立多个选项卡的时候,因为没有选项卡,所以不能切换. 弄了好一会儿才弄出来 点击会话选项卡或者Ctrl + Shift + T可以调出来
- 2017-18一《电子商务概论》专科作业--经管B1601/2、经管B1631
第1次作业: 1.你如何来定义和理解电子商务?电子商务对社会经济带了怎样的影响,企业.消费者的反应如何?你知道哪些电子商务企业,他们都属于什么类型? 2.请详细阐述应该如何关注哪些事项才能在淘宝网成功 ...
- 51Testing和传智播客相比哪个好?
首先我们需要先了解两家企业,51Testing是博为峰旗下的主营业务之一,主要是软件测试人才培训,包含就业培训.企业内训等服务,博为峰除了51Testing这个主营业务之外,还开设了51Code,主要 ...
- 再过两年C语言就50岁了,这么老的编程语言怎么还没有过时?
再过两年,C语言将迎来它的 50 岁生日,同样进行周年庆的还有 PL/M和Prolog.不过,C语言至今仍然非常受欢迎,它在几乎所有编程语言中的受欢迎程度,始终排在前十名. 大多数操作系统的内核( ...
- 【学习笔记】Min-max 容斥
经常和概率期望题相结合. 对于全序集合 \(S\),有: \[\max S=\sum\limits_{T\subseteq S,T\not=\varnothing}(-1)^{\vert T\vert ...
- HDU-1051 Wooden Sticks--线性动归(LIS)
题目大意:有n根木棍(n<5000),每根木棍有一个长度l和重量w(l,w<10000),现在要对这些木头进行加工,加工有以下规则: 1.你需要1分钟来准备第一根木头. 2.如果下一根木头 ...
- LUMEN框架多数据库连接配置方法
LUMEN作为一款API导向很浓的框架,配置极简化,默认只支持一路DB配置 然而随着业务复杂度的提高,引入多个数据库连接似乎无法避免,下面介绍一下LUMEN连接多个数据库的配置方法: 修改.env文件 ...
- Python开发 常见异常和解决办法
1.sqlalchemy创建外键关系报错property of that name exists on mapper SQLAlchemy是Python编程语言下的一款开源软件,提供了SQL工具包及对 ...
- centos7启用EPEL Repository
1,下载库文件 http://dl.fedoraproject.org/pub/epel/7/x86_64/Packages/e/epel-release-7-11.noarch.rpm 2,安装 r ...
- WAI-ARIA无障碍网页资料
一.ARIA是啥? WAI-ARIA指无障碍网页应用.主要针对的是视觉缺陷,失聪,行动不便的残疾人以及假装残疾的测试人员.尤其像盲人,眼睛看不到,其浏览网页则需要借助辅助设备,如屏幕阅读器,屏幕阅读机 ...