We adopt lowerCamelCase for both functions and variables, which is against the rule of Google C++ Style Guide and PEP 8 -- Style Guide for Python Code. However, to be consistent with the LeetCode OJ system, which uses lowerCamelCase for over 99% of the time, we stick with lowerCamelCase in this case. Remember the most important thing is to be consistent all the time.
We omit importing and brackets, and this is for shorter paragraph. In a real situation, retain importing let you quickly know where is this function/variable from and brackets make the indentation safer, so you might keep both of them.
Code can't be compiled, it's just for demonstrating purposes.
Even though there is auto keyword in C++ 11, for types like int and char, we tend to explicitly declare them. However, you might use auto most of the time in the real world.
We skip including.
We use int to traverse the array most of the time for simplicity. However, you should note the difference between size_t and int, and in the real world, that matters.
We omit std:: to access the standard library for a short paragraph.
Code can't be compiled, it's just for demonstrating purposes.
Even though there is var keyword in Java, for types like int and char, we tend to explicitly declare them so people can have an idea of what the type is at first glance.
We omit prefixes like collections. and math.. However, this might not be a good practice. Just want to keep the paragraph short.
We prefix private functions with _ and this might seem tedious. However, we tend to use something like Mypy and Pyre to do static type checking in the real world.
We pass the argument --indent-size=2 to autopep8 for a better viewing experience.
Declare the variables in the proper scope as slow as possible.
Declare const variables as soon as possible.
Declare ans as soon as possible.
Since LeetCode is just an online judge system rather than a big project, we don't scatter all variables in different sections. However, we still sort the variables based on the time we first use each of them.
Code section (there should be one blank line between each sections.)
public
boundary conditions
initial variables
There may be many kernels separated with one blank line, but there shouldn't be any blank line in each kernel.
classSolution{public:intlenLongestFibSubseq(vector<int>&A){// Only get the value of size or length// when we use it twice or more times.// Add `const`, and separate this line from next section a blank line.constintn=A.size();// Declare the variables in the proper scope as slow as possible.// Declare `ans` as soon as possible.// General Order:// ans -> dp -> STL -> pointers (TBD)//// Graph Order:// ans -> graph -> inDegree -> state -> q -> seenintans=0;vector<vector<int>>dp(n,vector<int>(n,2));unordered_map<int,int>numToIndex;for(inti=0;i<n;++i)numToIndex[A[i]]=i;for(intj=0;j<n;++j)for(intk=j+1;k<n;++k){constintai=A[k]-A[j];// use constif(ai<A[j]&&numToIndex.count(ai)){constinti=numToIndex[ai];// use constdp[j][k]=dp[i][j]+1;ans=max(ans,dp[j][k]);}}returnans;}};
// C++unordered_set<string>seen;unordered_map<char,int>count;// numToIndex, prefixToIndexvector<int>count;// sometimes it's a better choice than `unordered_map`stack<char>stack;queue<TreeNode*>q;deque<TreeNode*>q;autocompare=[](constListNode*a,constListNode*b){returna->val>b->val;};priority_queue<ListNode*,vector<ListNode*>,decltype(compare)>minHeap(compare);
# Pythonseen=set()# or wordSet = set() if you likecount={}count=collections.defaultdict(int)count=collections.defaultdict(list)count=collections.Counter()q=collections.deque([root])q=collections.deque([root])stack=[]minHeap=[]
Always prefer to one character to represent index variables.
Always prefer to use [l, r) pattern.
1 2 3 4 5 6 7 8 91011121314
intl=0;intr=nums.size();// or nums.size() - 1while(l<r){constintm=l+(r-l)/2;if(f(m))// optionalreturnm;// optionalif(g(m))l=m+1;// new range [m + 1, r)elser=m;// new range [l, m)}returnl;// nums[l]
ListNodedummy(0);// allocated on stack instead of heapListNode*curr;ListNode*prev;ListNode*next;ListNode*slow;ListNode*fast;ListNode*head;ListNode*tail;ListNode*l1;ListNode*l2;
// 2D Vectorconstintm=matrix.size();constintn=matrix[0].size();// if there're two stringsconstintm=str1.length();constintn=str2.length();// if there's only a stringconstintn=str.length();