[PYTHON] [Updated from time to time] LetCode algorithm and library

Always be aware

――Do you have a deep understanding of the technology you have touched on? --Paste the index to make it faster. ⇒ Why? ――People who have a shallow understanding of the concepts that they have touched on in the past will continue to work with a shallow understanding.

--To be able to understand and explain complex concepts

algorithm

Binary search tree (find L or more and R or less in depth-first search)

#Recursive function
# 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 rangeSumBST(self, root: TreeNode, L: int, R: int) -> int:
        valid_vals = []
        def search(root):
            if root.val >= L and root.val<=R:
                valid_vals.append(root.val)
            if root.left:
                search(root.left)
            if root.right:
                search(root.right)
        search(root)
        return sum(valid_vals)

Standard library

Base conversion

#Decimal number 2,8,Convert to hexadecimal
x = 10
print(bin(x))
print(oct(x))
print(hex(x))

# 2,8,Convert hexadecimal numbers to decimal numbers
print(int('10100', 2))
print(int('24', 8))
print(int('14', 16))

Uppercase / lowercase

s_org = 'pYThon proGramminG laNguAge'
s_org.upper()
# PYTHON PROGRAMMING LANGUAGE
s_org.lower()
# python programming language

zip is used with for statement

class Solution:
    def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
        cnt = 0
        for m, n in zip(startTime, endTime):
            if m <= queryTime and n >= queryTime:
                cnt += 1
        return cnt

List ascending / descending

l1 = ['d', 'b', 'c', 'a']
l2 = sorted(l1)
l2 = sorted(l1, reverse=True)
print(l2) # ['d', 'c', 'b', 'a']

Determine if the list is empty

a = []
if not a:
 print("The list is empty")

math library

ceiling function

Find the smallest integer greater than or equal to x for a real number x

from math import ceil
c = ceil(r)

Recommended Posts

[Updated from time to time] LetCode algorithm and library
vtkXMLUnstructuredGridReader Summary (updated from time to time)
vtkOpenFOAMReader Summary (Updated from time to time)
Engineer vocabulary (updated from time to time)
Tensorflow memo [updated from time to time]
Private Python handbook (updated from time to time)
vtkClipPolyData / DataSet Summary (Updated from time to time)
[Updated from time to time] PostmarketOS related notes
Summary of vtkThreshold (updated from time to time)
[Notes / Updated from time to time] This and that of Azure Functions
Summary of gcc options (updated from time to time)
Notes on machine learning (updated from time to time)
OpenFOAM post-processing cheat sheet (updated from time to time)
progate Python learning memo (updated from time to time)
Useful help sites, etc. (updated from time to time)
Machine learning python code summary (updated from time to time)
Apache settings, log confirmation, etc. (* Updated from time to time)
[Updated from time to time] Review of Let Code NumPy
I read the Chainer reference (updated from time to time)
Anaconda updated from 4.2.0 to 4.3.0 (python3.5 updated to python3.6)
Understand design patterns by comparing implementations in JavaScript and Java [Updated from time to time]
[Updated from time to time] Summary of design patterns in Java
Python (from first time to execution)
[Note] AI / machine learning / python related websites [updated from time to time]
Easy conversion from UTC to local time
Porting and modifying doublet-solver from python2 to python3.
Let Code Day 19 Starting from Zero "121. Best Time to Buy and Sell Stock"
[Kaggle] From data reading to preprocessing and encoding
[Python] How to read data from CIFAR-10 and CIFAR-100
Data retrieval from MacNote3 and migration to Write
To represent date, time, time, and seconds in Python
Updated to Python 2.7.9
I had a hard time trying to access Hadoop3.0.0 from a browser (and Arch Linux)
[Updated from time to time] Python memos often used for data analysis [N division, etc.]
Sum from 1 to 10
A memorandum of commands, packages, terms, etc. used in linux (updated from time to time)
(Updated from time to time) Storage location of various VS Code configuration files Memorandum memo
Convert timezoned date and time to Unixtime in Python2.7
I wanted to use the Python library from MATLAB
List of my articles that may be useful in competition pros (updated from time to time)