剑指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. ...
随机推荐
- Linux 下 svn 场景实例及常用命令详解
一.SVN使用场景实例 问题: 在使用svn做为版本控制系统的软件开发中,经常会有这样的需求:在工作复本目录树的不同目录中增加了很多文件,但未纳入版本控制系统,这时如果使用svn add命令一个一个的 ...
- DVWA渗透测试初级练习
下面的内容是我2020年后半年进行的简单的dvwa的渗透实验,顺序可能会有一些问题,但是内容我一定会搞完整,DVWA渗透环境的windows10配置phpstudy Command Injection ...
- Ubuntu 20.04上通过Wine 安装微信
没有想过会在一个手机软件上花这么多心思,好在今天总算安装成功,觉得可以记录下这个过程,方便他人方便自己. 首先介绍下我使用过的其他方法,希望可以节省大家一些时间: Rambox Pro:因为原理是网页 ...
- 同一台电脑同时使用gitHub和gitLab
工作中我们有时可能会在同一台电脑上使用多个git账号,例如:公司的gitLab账号,个人的gitHub账号.怎样才能在使用gitlab与github时,切换成对应的账号,并且免密?这时我们需要使用ss ...
- ttl转以太网
ttl转以太网 ttl转以太网ZLSN3007S是实现TTL电平串口转以太网的"超级网口",产品自带网络变压器和RJ45网口,可以方便实现单片机.各类TTL电平串口设备的联网.首先 ...
- day31 Pyhton 总结
# 什么是封装? # 广义上(大家认为的) : # 把一类事务的相同的行为和属性归到一个类中 # class Dog: # def bite(self):pass ...
- 阅读-Calibre Library转PDF、EPUB配置
提示:如果想恢复默认设置,点击"恢复默认值"即可 -----EPUB (MOBI同理)----- 目标:解决转换过程中图片清晰度丢失问题(分辨率太低) 右击-转换书籍-逐个转换 输 ...
- spring cloud:搭建基于consul的服务提供者集群(spring cloud hoxton sr8 / spring boot 2.3.4)
一,搭建基于consul的服务提供者集群 1,consul集群,共3个实例: 2, 服务提供者集群:共2个实例: 3,服务消费者:一个实例即可 4,consul集群的搭建,请参考: https://w ...
- C++ Primer第5版 第三章课后练习
练习3.1 #include <iostream> using namespace std; int main() { int sum = 0, val = 50; while (val ...
- 出Bug表-假如诸葛亮是程序员(1024程序员节日献礼)
1 前言 欢迎访问南瓜慢说 www.pkslow.com获取更多精彩文章! 出Bug表 南瓜言:先司创业未半而中道破产,今培训造才,网课套钱,此诚百家争鸣之时也.然优秀骨干组队离职,新招小白乐于摸鱼者 ...