lintcode 题目记录··

来源:互联网 时间:2017-06-01

单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod

class Solution: # @return: The same instance of this class every time __m_Solution=None @classmethod def getInstance(self): # write your code here if self.__m_Solution == None: self.__m_Solution=Solution() return self.__m_Solution def __init__(self): pass

遍历二叉树路径,输出与给定值相同的所有路径,这里是递归调用的,本来像把递归改成循环的 但是高了好久 没搞出来····

class Solution: # @param {int} target an integer # @return {int[][]} all valid paths def binaryTreePathSum(self, root, target): # Write your code here res = [] path = [] import copy def getallpath(current, path, res): if current == None: return path.append(current.val) if current.left != None: getallpath(current.left,copy.deepcopy(path), res) if current.right != None: getallpath(current.right,copy.deepcopy(path),res) if current.left is None and current.right is None: res.append(path) getallpath(root,path,res) ps=[] for p in res: sum=0 for v in p: sum=sum+v if sum==target:ps.append(p) return ps 

相关阅读:
Top