LeetCode brushing questions summary python3

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

When calling a function, add self.function name

list sort nums.sort()

**pre.next = l1 if l1 else l2 # Similar to C++ trinocular expression, l1 is true if l1 is true, otherwise l2 **

list list use

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 defines global variables

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 exchange

a, b = b, a # Similar to C++ swap operation

list simulation stack

self.stackB = []

if self.stackB: # Determine if it is empty

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

self.stackB.append(x) # push operation

self.stackB.pop() # pop() operation

Create 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)

# Output: deque([1, 2])

d.appendleft(2)

d.clear()

x = d.pop()

x = d.popleft()

d.reverse()

Recommended Posts

LeetCode brushing questions summary python3
Python interview questions summary
Python answers questions
Python interview questions
Python basic summary
Python processing json summary
Python advanced usage summary
Python high-order function usage summary!
Python datetime processing time summary
Leetcode 2 add two numbers Python
Python classic interview questions two
Python interview questions collection (three)
Python high-order function usage summary!
Summary of logarithm method in Python
Python list comprehension operation example summary
A summary of 200 Python standard libraries!
Summary of Python calling private attributes
Python decorator simple usage example summary
Python classic programming questions: string replacement