首页 > 编程语言 >leetcode python 解题模板

leetcode python 解题模板

时间:2022-09-25 04:22:05浏览次数:47  
标签:node right TreeNode python self root leetcode 模板 left

一般的题

class Solution(object):
    def letterCombinations(self, digits):
        """
        :type digits: str
        :rtype: List[str]
        """
        dic = { "2": "abc", "3": "def", "4":"ghi", "5":"jkl", "6":"mno", "7":"pqrs", "8":"tuv", "9":"wxyz"}
        
        res=[]
        if len(digits) ==0:
            return res
            
        self.dfs(digits, 0, dic, '', res)
        return res
    
    def dfs(self, nums, index, dic, path, res):
        if index >=len(nums):
            res.append(path)
            return
        string1 =dic[nums[index]]
        for i in string1:
            self.dfs(nums, index+1, dic, path + i, res)
            
solution = Solution()
print(solution.letterCombinations("23"))
#这个self可以不用传入,不管就行了

 涉及到树的题(参考:https://blog.csdn.net/qq_43355165/article/details/122780188)

class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    # 1. 递归
    def mirrorTree_1(self, root):
        """
        :type root: TreeNode
        :rtype: TreeNode
        """
        if not root: return
        # tmp = root.left
        # root.left = self.mirrorTree(root.right)
        # root.right = self.mirrorTree(tmp)
        root.left, root.right = self.mirrorTree(root.right), self.mirrorTree_1(root.left)
        return root
    # 2. 辅助栈
    def mirrorTree(self, root):
        if not root: return
        stack = [root]
        while stack:
            node = stack.pop()
            if node.left: stack.append(node.left)
            if node.right: stack.append(node.right)
            node.left, node.right = node.right, node.left
        return root

t1 = TreeNode(
4
)
t2 = TreeNode(
2
)
t3 = TreeNode(
7
)
t4 = TreeNode(
1
)
t5 = TreeNode(
3
)
t6 = TreeNode(
6
)
t7 = TreeNode(
9
)

t2.left = t4
t2.right = t5
t3.left = t6
t3.right = t7
t1.left = t2
t1.right = t3
solution = Solution()
a = solution.mirrorTree(t1)
print(a.__dict__)

 

标签:node,right,TreeNode,python,self,root,leetcode,模板,left
From: https://www.cnblogs.com/immiao0319/p/16727153.html

相关文章

  • Liunx环境变量配置模板
    目录jdk环境jdk环境vim/etc/profileexportJAVA_HOME=/opt/javaexportJRE_HOME=$JAVA_HOME/jreexportCLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATHexport......
  • python基础学习01
     1.python的两个变量互换值时不需要建立临时变量,x,y=y,x即可实现x和y的值的互换。 2.print('\"lifeisshort,let\'slearnpython\"')使用转义字符可以避免引号配对......
  • LeetCode - 数组的改变和移动
    1.数组的改变和移动总结1.1数组的改变数组在内存中是一块连续的内存空间,我们可以直接通过下标进行访问,并进行修改。在Java中,对于List类型来说,我们可以通过set(idx,el......
  • Python项目管理: Poetry
    Python项目管理:Poetry1.导读本文将介绍一个目前十分流行,且用于众多Python项目中依赖管理和打包的工具,包含基本的安装与使用。2.PoetryPoetry是Python中用于依赖......
  • Python4-eg
    实例01importdatetime                        #导入日期时间类#定义一个列表mot=["今天星期一:\n坚持下去不是因为我很坚强,而是因为我别无......
  • python代码编写---PEP8规范
    PEP8规范---StyleGuideforPythonCodePEP8简介1.PEP是PythonEnhancementProposal的缩写,通常翻译为:Python增强提案enhance增强改善(V)Proposal提议建议(n)2.......
  • Python 判断字符串是否包含中文
    一、原理中文字符的编码范围是:\u4e00-\u9fff只要编码在此范围就可判断为中文字符 二、函数defis_chinese(self,string):"""检查整个字符串是否包......
  • Python常用标准库之os
    模块导入方式:importosos模块是Python标准库中的一个用于访问操作系统相关功能的模块,os模块提供了一种可移植的使用操作系统功能的方法。使用os模块中提供的接口,可以实现......
  • Python 模型超参数调优
    Python模型超参数调优1.导读本文将对超参数进行简要的解释,并推荐一本利用Python进行超参数调整的书籍,其中包含了许多超参数调整方法。2.超参数在机器学习的上下文中......
  • Python cv2(Opencv) 图像运算
    OpenCV是一个基于Apache2.0许可(开源)发行的跨平台计算机视觉和机器学习软件库,可以运行在Linux、Windows、Android和MacOS操作系统上。它轻量级而且高效,由一系列C函数和少......