前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode刷题总结python3

LeetCode刷题总结python3

作者头像
用户7787521
发布2020-09-25 12:30:09
2680
发布2020-09-25 12:30:09
举报
文章被收录于专栏:蒋豆芽的宝箱蒋豆芽的宝箱

# Definition for a binary tree node.

# class TreeNode:

# def __init__(self, x):

# self.val = x

# self.left = None

# self.right = None

# Definition for singly-linked list.

# class ListNode:

# def __init__(self, x):

# self.val = x

# self.next = None

调用函数时,加 self.函数名

list排序 nums.sort()

pre.next = l1 if l1 else l2 # 类似C++三目表达式,l1为真则l1,否则l2

list列表使用

def convertBST(self, root: TreeNode) -> TreeNode:

stack = [(root,1)]

sum = 0

while stack:

node, cmd = stack.pop()

if node is None:

continue

if cmd == 0:

sum += node.val

node.val = sum

else:

stack.append((node.left, 1))

stack.append((node, 0))

stack.append((node.right, 1))

return root

python定义全局变量

self.sum =0

def convertBST(self, root: TreeNode) -> TreeNode:

if root == None:

return

self.val = 0

def dfs(root):

if root == None:

return

root.right = dfs(root.right)

self.val += root.val

root.val = self.val

root.left = dfs(root.left)

return root

return dfs(root)

python3交换

a, b = b, a # 类似C++ swap操作

list模拟栈

self.stackB = []

if self.stackB: # 判断是否为空

self.stackB[-1] # 取top()

self.stackB.append(x) # push操作

self.stackB.pop() # pop()操作

创建list

ary_len =len(prices)

dp= [0] * ary_len

import collections

d = collections.deque()

import collections

d = collections.deque()

d.append(1)

d.append(2)

print(d)

#输出:deque([1, 2])

d.appendleft(2)

d.clear()

x = d.pop()

x = d.popleft()

d.reverse()

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
命令行工具
腾讯云命令行工具 TCCLI 是管理腾讯云资源的统一工具。使用腾讯云命令行工具,您可以快速调用腾讯云 API 来管理您的腾讯云资源。此外,您还可以基于腾讯云的命令行工具来做自动化和脚本处理,以更多样的方式进行组合和重用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档