lintCode 入门题 python版解答

献给我的人生第二春,下面开始问题与答案

  1. 矩阵面积

class Rectangle():
    '''
     * Define a constructor which expects two parameters width and height here.
    '''
    width = 0.1
    height= 0.1
    # write your code here
    def __init__(self, width,height):
        self.width = width
        self.height = height
        '''
     * Define a public method `getArea` which can calculate the area of the
     * rectangle and return.
    '''
    # write your code here
    def getArea(self):
        return self.width*self.height

2.二叉树遍历

二叉树原理点击打开链接

其它解答http://www.cnblogs.com/bozhou/p/LintCode.html

  LintCode已实现NodeTree类,传入的参数实际为一个NodeTree类型的二叉树,没有找到方法接触源码,只能通过网页一步一步调试,分析出结构:
其中,Node节点类拥有三个属性
   left:当前节点的左节点
  right:当前节点的右节点
  val:当前节点的值
class Solution:
    """
    @param: root: the root of tree
    @return: the max node
    """
    maxVal = -9999
    node = None
    # write your code here
    def maxNode(self, root):
        if root == None:
            return None
        self.max(root)
        return self.node
    
    def max(self,node):
        if node == None:
            return None
        if node.val > self.maxVal:
            self.node = node
            self.maxVal = node.val
        self.max(node.left)
        self.max(node.right)

3 ,整数排序

class Solution:
    """
    @param: A: an integer array
    @return: 
    """
    def sortIntegers(self, A):
        # write your code here
        for i in range(len(A)-1):
            for j in range(len(A)-i-1):
                if A[j]>A[j+1]:
                    B=A[j]
                    A[j]=A[j+1]
                    A[j+1]=B
        return A




你可能感兴趣的:(lintCode 入门题 python版解答)