Leetcode
1. 多行注释:ctrl + /
2. enumerae()函数
将一个可遍历的数据对象组合为一个索引序列,同时列出数据和数据下标,如:
seq = ['one', 'two', 'three'] for i, element in enumerate(seq): print(i, element)
0 one
1 two
3 three
3. find shortest string in list, key=len可忽略
min(list_name, key = len)
4. 取余数:%,整除(向下取整)://
5. Tree
Binary Search Tree作用:speed up our binaryy search as we are able to find items faster
Tree traversal: traversing means visiting and outputting the value of each node in a particular order
*3种Depth First Traversals*
a) In-order: In the case of binary search trees (BST), Inorder traversal gives nodes in non-decreasing order
Until all nodes are traversed-
Step 1: recursively traverse left subtree
Step 2: visit root node
Step 3: recursively traverse right subtree
b) Pre-order: Preorder traversal is used to create a copy of the tree. Preorder traversal is also used to get prefix expression on an expression tree
Until all nodes are traversed- Step 1: visit root node Step 2: recursively traverse left subtree
Step 3: recursively traverse right subtree
c) Post-order: Postorder traversal is used to delete the tree or get the postfix expression of an expression tree
Until all nodes are traversed- Step 1:recursively traverse left subtree
Step 2: recursively traverse right subtree
Step 3: visit root node
Depth First Traversals:
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
6. 冒泡排序
def bubble_sort(nums): N = len(nums) for i in range(N - 1): for j in range(N - 1 - i): if nums[j] > nums[j + 1]: nums[j], nums[j + 1] = nums[j + 1], nums[j] return nums