您的位置:首页 > 编程语言 > Python开发

lintCode 入门题 python版解答

2017-10-24 11:06 344 查看
献给我的人生第二春,下面开始问题与答案

矩阵面积

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: