《剑指Offer》----编程题之Python实现(共66题)13-18

题13:(调整数组顺序使奇数位于偶数前面)
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
# -*- coding:utf-8 -*-
class Solution:
    def reOrderArray(self, array):

        array_1=[]
        array_2=[]
        for i in range(len(array)):
            if array[i] % 2 == 0:
                array_1.append(array[i])
            else:
                array_2.append(array[i])
        array_2.extend(array_1)
        return array_2

题14:(链表中倒数第k个节点)
输入一个链表,输出该链表中倒数第k个结点。
# -*- coding:utf-8 -*-
class ListNode:
    def __init__(self, x):

        self.val = x
        self.next = None
class Solution:
    def FindKthToTail(self, head, k):

        if k<=0 or head==None:
            return None
        count=0
        p=head
        while p!=None:
            count=count+1
            p=p.next
        if count<k:
            return None
        number=count-k+1
        cnt=0
        p=head
        while p!=None:
            cnt=cnt+1
            if cnt==number:
                return p
            p=p.next

题15:(反转链表)
输入一个链表,反转链表后,输出链表的所有元素。
# -*- 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:
            return None
        else:
            if pHead.next:
                head = self.ReverseList(pHead.next)
                pHead.next.next = pHead
                pHead.next = None
                return head
            else:
                return pHead

题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
        dummy = ListNode(0)
        pHead = dummy
       
        while pHead1 and pHead2:
            if pHead1.val >= pHead2.val:
                dummy.next = pHead2
                pHead2 = pHead2.next
            else:
                dummy.next = pHead1
                pHead1 = pHead1.next
           
            dummy = dummy.next
       
        if pHead1:
            dummy.next = pHead1
        elif pHead2:
            dummy.next = pHead2
        return pHead.next

题17:(树的子结构)
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
# -*- 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):

        def convert(p):
            if p:
                return str(p.val) + convert(p.left) + convert(p.right)
            else:
                return ""
        return convert(pRoot2) in convert(pRoot1) if pRoot2 else False

题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 not root:
            return root
        node = root.left
        root.left = root.right
        root.right=node
        self.Mirror(root.left)
        self.Mirror(root.right)
        return root

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值