1. Sum of three numbers
Source: LeetCode
Link: https://leetcode-cn.com/problems/3sum
Code: learn Wu Yanzu's solution
Give you an array num containing n integers. Judge whether there are three elements a, b and c in num, so that a + b + c = 0? Please find all triples with sum 0 and no repetition.
class Solution: def threeSum(self, nums: List[int]) -> List[List[int]]: n = len(nums) #Find list length res = [] #Store in a place that meets the requirements if (not nums) or (n<3): #If the array is null or the array length is less than 3 return [] #Returns [] nums.sort() #Sort array if nums[0]>0: #Because the list is sorted well, when the first number is > 0, it cannot be greater than 0 after three numbers return [] for i in range(n): #Traverse the sorted array if i>0 and nums[i]==nums[i-1]: #Prevent duplicate elements continue L=i+1 #Set left pointer R=n-1 #Set right pointer while(L<R): if(nums[i]+nums[L]+nums[R]==0): #If the sum of three is equal to 0 res.append([nums[i],nums[L],nums[R]) #Add three numbers to the res list while(L<R and nums[L]==nums[L+1]): #Execute a loop to determine whether the left boundary repeats with the next position L=L+1 while(L<R and nums[R]==nums[R-1]): #Execute the loop to determine whether the right loop is repeated with the next position R=R-1 #Move L and R to the next position to find a new solution L = L+1 R = R-1 elif(nums[i]+nums[L]+nums[R])>0): #If the sum of the three numbers is greater than 0, the right value is too large. R should be smaller and move to the left R = R-1 else: #If the sum of the three numbers is less than 0, the left value is too small. L should be a little larger and move to the right L = L+1 return res
2. Put apples
Source: niuke.com
Put m same apples on n same plates, and some plates are allowed to remain empty. How many different methods are there? (represented by K) 5, 1, 1 and 1, 5, 1 are the same division.
def f(m,n): if m<0 or n<0: return 0 elif m==1 or n==1: return 1 else: #There are two situations for putting apples. One is that there are empty plates, and the other is that there are apples on each plate #f(m,n-1) is the method for a plate to be empty #f(m-n,n) is that there are apples on each plate return f(m,n-1)+f(m-n,n) while True: try: m,n=map(int,input().split()) #m is the apple and n is the plate print(f(m,n)) except: break
3. Recursion
Source: geek time - the beauty of data structure and algorithm
PS: This is a study note
The key of recursive code is to find the law of how to decompose a large problem into a small problem, write a recursive formula based on this, then refine the termination conditions, and finally translate the recursive formula and termination conditions into code.
There are three conditions for recursion:
- The solution of a problem can be decomposed into the solutions of several subproblems
- This problem is exactly the same as the sub problem after decomposition, except that the data scale is different
- Recursive termination condition exists
Example: climbing stairs
How many ways to climb stairs are there for climbing one or two stairs, n steps?
All walking methods can be divided into two categories according to the first step. The first is that the first step has taken one step, and the other is that the first step has taken two steps. Therefore, the walking method of N steps is equal to the walking method of n-1 steps after walking 1 step first, plus the walking method of n-2 steps after walking 2 steps first
f(n) = f(n-1)+f(n-2)
The termination condition is f(1)=1. If there are two steps, f(2)=f(1)+f(0), but there is no f(0), so the termination condition includes f(2)=2
4. Database learning
Source: look through your previous notes. The previous notes seem to look at which learning document you forgot before
Fuzzy query
select Listing from Table name where Listing like condition; % Represents 0 or more arbitrary characters _Represents any 1 character
Aggregate (grouping) function
count(Listing) Statistics sum(Listing) Sum avg(Listing) Average min(Listing) Find the minimum value max(Listing) Find the maximum value
Grouping query
select Listing/Aggregate function from Table name where condition group by Listing order by Column name 1/Aggregate function asc/desc;
having statement
After the grouping query results are obtained, the data is filtered again select Listing/Aggregate function from Table name where condition group by Listing having condition having Indicates that the grouped array is filtered and cannot be used alone. It must be combined with group by Use together
Sub (nested) query
It's in one SQL Statement contains another SQL sentence For example: query emp In the table, if the salary is greater than the average salary, the employee's number, name, position and salary 1,First, find the average wage select avg(sal) from emp; 2,synthesis select empno,ename,job,sal from emp where sal>(select avg(sal) from emp);
join query connection usage
inner join Internal connection to obtain the records of the field matching relationship in the two tables left join Get all records in the left table, even if there is no corresponding matching record in the right table right join Get all records in the right table, even if there is no corresponding matching record in the left table