剑指offer练习Python(二)

来源:互联网 发布:淘宝包邮退货邮费规则 编辑:程序博客网 时间:2024/06/07 04:07

http://blog.csdn.net/u011274209/article/details/61011777

(12)数值的整数次方

           给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

# -*- coding:utf-8 -*-class Solution:    def Power(self, base, exponent):        # write code here        import math        return pow(base, exponent)

(13) 调整数组顺序使奇数位于偶数前面

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

# -*- coding:utf-8 -*-class Solution:    def reOrderArray(self, array):        # write code here        a = []        b = []        for i in array:            if (i%2):                a.append(i)            else:                b.append(i)        return a+b
(14)链表中倒数第k个结点

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

# -*- coding:utf-8 -*-# class ListNode:#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution:    def FindKthToTail(self, head, k):        # write code here        l=[]        while head!=None:            l.append(head)            head = head.next        if k > len(l) or k < 1:            return        return l[-k]            

(15)反转链表

           输入一个链表,反转链表后,输出链表的所有元素。

# -*- coding:utf-8 -*-# class ListNode:#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution:    # 返回ListNode    def ReverseList(self, pHead):        # write code here        if not pHead or not pHead.next:            return pHead        pre = pHead        next = pHead.next.next        pHead = pHead.next        pre.next = None        while next != None:            pHead.next = pre            pre = pHead            pHead = next            next = next.next        pHead.next = pre        return pHead

(16)合并两个排序的链表

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

# -*- coding:utf-8 -*-# class ListNode:#     def __init__(self, x):#         self.val = x#         self.next = Noneclass Solution:    # 返回合并后列表    def Merge(self, pHead1, pHead2):        # write code here        if pHead1 is None:        return pHead2        if pHead2 is None:        return pHead1        if pHead1.val > pHead2.val:        head = pHead2        pHead2 = pHead2.next        else:        head = pHead1        pHead1 = pHead1.next        p = head        while pHead1 is not None and pHead2 is not None:        if pHead1.val > pHead2.val:        head.next = pHead2        pHead2 = pHead2.next        else:        head.next = pHead1        pHead1 = pHead1.next        head = head.next        if pHead1 is not None:        head.next = pHead1        if pHead2 is not None:        head.next = pHead2        return p
(17)树的子结构

           输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

# -*- coding:utf-8 -*-# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution:    def HasSubtree(self, pRoot1, pRoot2):        # write code hereif pRoot1 is None or pRoot2 is None:return Falseif pRoot1.val == pRoot2.val:leftBool = TruerightBool = True #左右子树都不存在,值一样,也是Trueif pRoot2.left is not None:leftBool = self.HasSubtree(pRoot1.left, pRoot2.left)if pRoot2.right is not None:rightBool = self.HasSubtree(pRoot1.right, pRoot2.right)if leftBool and rightBool:return TrueleftBool = self.HasSubtree(pRoot1.left, pRoot2)rightBool = self.HasSubtree(pRoot1.right, pRoot2)if leftBool or rightBool:return Truereturn 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 = Noneclass 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)        return root

(19)顺时针打印矩阵

          输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10.

# -*- coding:utf-8 -*-class Solution:    # matrix类型为二维列表,需要返回列表    def printMatrix(self, matrix):        # write code here        l = []        top = left = 0        right = len(matrix[0]) - 1        bottom = len(matrix) - 1        while top <= bottom and left <= right:            for i in range(left, right + 1):                l.append(matrix[top][i])            for i in range(top + 1, bottom + 1):                l.append(matrix[i][right])            if bottom is not top:                             for i in range(left, right)[::-1]:                     l.append(matrix[bottom][i])            if right is not left:                           for i in range(top + 1, bottom)[::-1]:                     l.append(matrix[i][left])            top += 1            bottom -= 1            left += 1            right -= 1        return l
(20)包含min函数的栈

           定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数。

# -*- coding:utf-8 -*-class Solution:    def __init__(self):        self.stack = []        self.minstack = []    def push(self, node):        # write code here        min = self.min()        if not min or node < min:            self.minstack.append(node)        else:            self.minstack.append(min)        self.stack.append(node)    def pop(self):        # write code here        if self.minstack.pop():            return self.stack.pop()    def top(self):        # write code here        if self.stack:            return self.stack[-1]    def min(self):        # write code here        if self.minstack:            return self.minstack[-1]
(21)栈的压入、弹出序列

           输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。(注意:这两个序列的长度是相等的)

# -*- coding:utf-8 -*-class Solution:    def IsPopOrder(self, pushV, popV):        # write code here        stack = []        for i in pushV:            stack.append(i)            while len(stack) and stack[-1] == popV[0]:                stack.pop()                popV.pop(0)        if len(stack):            return False        return True

(22)从上到下打印二叉树

           从上往下打印出二叉树的每个节点,同层节点从左至右打印。

# -*- coding:utf-8 -*-# class TreeNode:#     def __init__(self, x):#         self.val = x#         self.left = None#         self.right = Noneclass Solution:    # 返回从上到下每个节点值列表,例:[1,2,3]    def PrintFromTopToBottom(self, root):        # write code here        if root is None:            return []        q = []        result = []        q.append(root)        while len(q):            node = q.pop(0)            result.append(node.val)            if node.left:                q.append(node.left)            if node.right:                q.append(node.right)        return result



0 0
原创粉丝点击