剑指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. ...
随机推荐
- 项目使用eslint
今天eslint版本更新了,然后昂,有些奇奇怪怪的错误提示了,然后想,这我得 1.配置一个保存时根据eslint规则自动修复 2.欸,之前编码遇到未使用的变量都会有标记黄线,我很好定位,这会怎么没了 ...
- tmpwatch命令清除旧文件
我们知道系统里面常常会有一些忘记删除的长时间不用而且确实没有用处的文件,如果不去处理,这些无用的文件会越来越多,浪费许多系统资源.在不知道文件名的情况下,很难去检查某一目录下到底是哪些文件长时间没有被 ...
- shell-变量的数值运算let内置命令
1. let命令的用法 格式: let 赋值表达式 [注]let赋值表达式功能等同于:((赋值表达式)) 范例1:给自变量i加8 [root@1-241 scripts]# i=2 [root@1- ...
- turtle库元素语法分析
一.turtle原理理解: turtle库是Python中一个有趣的图形绘制函数库.原名(海龟),我们想象一只海龟,位于显示器上窗体的正中心,在画布上游走,它游走的轨迹就形成了绘制的图形. 对于小海龟 ...
- .Net Core单元测试规范
.Net Core单元测试规范 一. 前言 为了有效提升代码质量,保证DevOps的顺利进行.将全面开始采用单元测试进行覆盖,届时单元测试将完全纳入 到完整的持续构建生命周期中做为第一道质量把控的门槛 ...
- printStackTrace()造成的并发瓶颈
一 背景 在一次活动前的压测中,发现一个服务(平响为250ms左右)存在并发瓶颈,单实例的QPS压力从20升高到40后服务就雪崩了(平响急剧升高). 通过<jstack -F>命令查看线程 ...
- C# POst 接收或发送XML
摘自:http://www.cnblogs.com/Fooo/p/3529371.html 项目分成两个 web(ASP.Net)用户处理请求,客户端(wpf/winform)发送请求 1.web项目 ...
- matlab cvx工具箱解决线性优化问题
题目来源:数学建模算法与应用第二版(司守奎)第一章习题1.4 题目说明 作者在答案中已经说明,求解上述线性规划模型时,尽量用Lingo软件,如果使用Matlab软件求解,需要做变量替换,把二维决策变量 ...
- poj1654 -- Area (任意多边形面积)
Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20444 Accepted: 5567 Description ...
- poj1050 To the Max(降维dp)
To the Max Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 49351 Accepted: 26142 Desc ...