leetcode杂交刷题之始 -- Python3
2022/9/16 14:17:15
本文主要是介绍leetcode杂交刷题之始 -- Python3,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
- 二叉树开始 使用Python3 完成前中后序遍历
点击查看代码
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]: stack, rst = [root], [] while stack: i = stack.pop() if isinstance(i, TreeNode): stack.extend([i.right, i.val, i.left]) elif isinstance(i, int): rst.append(i) return rst
注 : Optional[] -> Optional[int] == Optional[int, None]
防止在传入参数为None时进行报错
2. 开始经典两数之和
点击查看代码
# 直接拷贝最easy的方法 class Solution: def twoSum(self, nums: List[int], target: int) -> List[int]: hashtable = dict() for i, num in enumerate(nums): if target - num in hashtable: return[hashtable[target-num], i] hashtable[num] = i return []
这篇关于leetcode杂交刷题之始 -- Python3的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-03-30开始python成长之路
- 2024-03-29python optparse
- 2024-03-29python map 函数
- 2024-03-20invalid format specifier python
- 2024-03-18pool.map python
- 2024-03-18threads in python
- 2024-03-14python Ai 应用开发基础训练,字符串,字典,文件
- 2024-03-13id3 algorithm python
- 2024-03-13sum array elements python
- 2024-03-12python colon equals