P13 调整数组顺序使奇数位于偶数前面
题目描述
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
# -*- coding:utf-8 -*-
class Solution:
def reOrderArray(self, array):
# write code here
odd_number=[]
even_number=[]
for i in range(len(array)):
if array[i]%2==0:
even_number.append(array[i])
else:
odd_number.append(array[i])
return odd_number+even_number
P14 链表中倒数第k个节点
题目描述
输入一个链表,输出该链表中倒数第k个结点。
注意:
1、要考虑到k大于链表长度和小于1的情况
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def FindKthToTail(self, head, k):
# write code here
chain=[]
while head:
chain.append(head)
head=head.next
if k<1 or k>len(chain):
return
return chain[-k]
P15 反转链表
题目描述
输入一个链表,反转链表后,输出新链表的表头。
注意:
1、要考虑到pHead为空或者链表中只有它一个节点的情况
2、判断条件中要先判断pHead是否为空在判断再下一节点是否为空,不然会报错“没有next这一属性”
3、要记得把第一个节点的下一节点赋空值
# -*- coding:utf-8 -*-
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
# 返回ListNode
def ReverseList(self, pHead):
# write code here
if not pHead or not pHead.next:
return pHead
next_node=pHead
front_node=pHead.next
pHead.next=None
while front_node.next:
temp_node=front_node.next
front_node.next=next_node
next_node=front_node
front_node=temp_node
front_node.next=next_node
return front_node
P16 合并两个排序的链表
题目描述
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
注意:
1、函数内部调用自身要有self.XXX
2、递归的思想
# -*- 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 not pHead1:
return pHead2
if not pHead2:
return pHead1
merged_list=None
if pHead1.val<pHead2.val:
merged_list=pHead1
merged_list.next=self.Merge(pHead1.next,pHead2)
else:
merged_list=pHead2
merged_list.next=self.Merge(pHead1,pHead2.next)
return merged_list
P17 树的子结构
题目描述
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
注意:
1、依然是递归的思想
2、已经指定判断B是否是子结构
# -*- 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
if not pRoot1 or not pRoot2:
return False
return self.is_Subtree(pRoot1,pRoot2) or self.HasSubtree(pRoot1.left,pRoot2) or self.HasSubtree(pRoot1.right,pRoot2)
def is_Subtree(self,pRoot1,pRoot2):
if not pRoot2:
return True
if not pRoot1:
return pRoot1==pRoot2
if pRoot1.val==pRoot2.val:
return self.is_Subtree(pRoot1.right,pRoot2.right) and self.is_Subtree(pRoot1.left,pRoot2.left)
else:
return False or self.is_Subtree(pRoot1.right,pRoot2) or self.is_Subtree(pRoot1.left,pRoot2)
P18 二叉树的镜像
题目描述
操作给定的二叉树,将其变换为源二叉树的镜像。
输入描述:
二叉树的镜像定义:源二叉树
8
/ \
6 10
/ \ / \
5 7 9 11
镜像二叉树:
8
/ \
10 6
/ \ / \
11 9 7 5
注意:
1、递归的思想
2、交换的时候不用引入temp变量,可以通过直接赋值交换两个变量的值
python采用引用语义的方式,存储的只是一个变量的值所在的内存地址,而不是这个变量的值本身
(1)直接赋值,默认浅拷贝传递对象的引用而已,原始列表改变,被赋值的b也会做相同的改变
>>> b=alist
>>> print b
[1, 2, 3, ['a', 'b']]
>>> alist.append(5)
>>> print alist;print b
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b'], 5]
(2)copy浅拷贝,没有拷贝子对象,所以原始数据改变,子对象会改变
>>> import copy
>>> c=copy.copy(alist)
>>> print alist;print c
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]
>>> alist.append(5)
>>> print alist;print c
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b']]
>>> alist[3]
['a', 'b']
>>> alist[3].append('cccc')
>>> print alist;print c
[1, 2, 3, ['a', 'b', 'cccc'], 5]
[1, 2, 3, ['a', 'b', 'cccc']] 里面的子对象被改变了
(3)深拷贝,包含对象里面的自对象的拷贝,所以原始对象的改变不会造成深拷贝里任何子元素的改变
>>> import copy
>>> d=copy.deepcopy(alist)
>>> print alist;print d
[1, 2, 3, ['a', 'b']]
[1, 2, 3, ['a', 'b']]始终没有改变
>>> alist.append(5)
>>> print alist;print d
[1, 2, 3, ['a', 'b'], 5]
[1, 2, 3, ['a', 'b']]始终没有改变
>>> alist[3]
['a', 'b']
>>> alist[3].append("ccccc")
>>> print alist;print d
[1, 2, 3, ['a', 'b', 'ccccc'], 5]
[1, 2, 3, ['a', 'b']] 始终没有改变
via: https://www.cnblogs.com/xueli/p/4952063.html
# -*- 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 None
root.left,root.right=root.right,root.left
if root.left:
self.Mirror(root.left)
if root.right:
self.Mirror(root.right)