python-剑指offer11-15

Posted 西西嘛呦

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python-剑指offer11-15相关的知识,希望对你有一定的参考价值。

11、

输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。

class Solution:
    def reOrderArray(self, array):
        # write code here
        for i in range(len(array)):
            for j in range(len(array)-1,i,-1):
                if array[j] % 2 == 1 and array[j - 1] % 2 == 0:
                    array[j],array[j-1] = array[j-1],array[j]
        return array

12、链表

输入一个链表,输出该链表中倒数第k个结点。

# -*- 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
        if head == None or k == 0:
            return None
        p=head
        l=0
        while p:
            l+=1
            p=p.next
        if l<k:
            return None
        p1=head
        p2=head
        while k!=0:
            k-=1
            p2=p2.next
        while p2:
            p2=p2.next
            p1=p1.next
        return p1

解题思路:双指针。

另一种思路:使用栈

# -*- 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
        if (head == None or k == 0):
            return None
        count = 0
        stack = []
        while head != None:
            stack.append(head)
            head = head.next
            count += 1
        if (count < k):
            return None
        for i in range(k):
            res = stack.pop()
        return res

13、链表

输入一个链表,反转链表后,输出新链表的表头

# -*- 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 pHead.next == None:
            return pHead
        p1=None
        p2=pHead
        while p2:
            cur=p2.next
            p2.next=p1
            p1=p2
            p2=cur
        return p1

14、链表

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

# -*- 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 or pHead2):
            return None
        elif not pHead1:
            return pHead2
        elif not pHead2:
            return pHead1
        c1 = pHead1
        c2 = pHead2
        head = ListNode(0)
        c3 = head
        while c1 and c2:
            if c1.val <= c2.val:
                c3.next = c1
                c3 = c1
                c1 = c1.next
            else:
                c3.next = c2
                c3 = c2
                c2 = c2.next
        if c1:
            c3.next = c1
        if c2:
            c3.next = c2
        return head.next

15、树

输入两棵二叉树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):
        result = False
        if pRoot1 and pRoot2:
            if pRoot1.val == pRoot2.val:
                result = self.check_structure(pRoot1, pRoot2)
            if not result:
                result = self.HasSubtree(pRoot1.left, pRoot2)
            if not result:
                result = self.HasSubtree(pRoot1.right, pRoot2)
        return result
    def check_structure(self, root1, root2):
        if not root2:
            return True
        if not root1:
            return False
        if root1.val != root2.val:
            return False
        left_check = self.check_structure(root1.left, root2.left)
        right_check = self.check_structure(root1.right, root2.right)
        return left_check and right_check

 

以上是关于python-剑指offer11-15的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode刷题Python剑指 Offer 04. 二维数组中的查找

《剑指offer》:[41]数组中和为S的两个数

剑指offer---和为S的两个数字

剑指Offer算法类题目[Python版]

剑指Offer算法类题目[Python版]

剑指offer反转链表python