This style guide is specifically for LeetCode and is based on Google style
guides with adjustments specific to LeetCode. It aims to maintain consistency
across different programming languages while adhering to LeetCode's conventions.
Use lowerCamelCase for both functions and variables. While this may deviate
from the rules laid out in the
Google C++ Style Guide
and
Google Python Style Guide,
it is done to maintain consistency with the LeetCode OJ system, which employs
lowerCamelCase for over 99% of the time. Remember, the most important thing
is to be consistent at all times.
Import statements and brackets () may be omitted for better viewing
experience on mobile devices, but they should be retained in real-world
coding situations for clarity and maintainability.
The code is only for demonstration and cannot be compiled.
#include statements are omitted in code snippets for brevity.
Understanding the difference between size_t and int is crucial when
traversing arrays in practical applications, as it can affect the outcome.
Qualifying with std:: prefix when accessing standard libraries is
recommended in real-world, as it clearly indicates the source of the
identifier being used and avoid name conflicts.
Private functions are prefixed with _, which may seem tedious. However, in
the real world, we use tools like Mypy,
Pytype, and
Pyre for static type checking.
We pass the argument --indent-size=2 to
autopep8 for a better viewing
experience on mobile devices.
import statements are omitted in code snippets for brevity.
Variables like next and hash are reserved keywords. For consistency with
C++ and Java naming conventions, we'll use them. However, in practice, using
reserved keywords as variable names can cause confusion and issues. It's
better to choose names that avoid such conflicts.
The naming of LeetCode is quite a mess and inconsistently uses plurals and
singulars together. I'll use my best judgment to name tables in
UpperCamelCase in the plural form and fields in lower_snake_case.
Proper indentation will be taken care of and treated seriously.
Declare the variables in the proper scope as slow as possible.
Declare the constants as soon as possible.
Declare ans as soon as possible.
No blank lines between variables initialization.
Blank one single line between each main section.
If the last statement is not a paragraph (for loop most of the case), then
no blank lines between it and the return statement.
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.
Set<String>seen=newHashSet<>();Map<Character,Integer>count=newHashMap<>();int[]count=newint[n];Deque<Character>stack=newArrayDeque<>();// Do not use `Stack`.Queue<Integer>q=newLinkedList<>();Deque<Integer>minQ=newArrayDeque<>();Queue<ListNode>minHeap=newPriorityQueue<>((a,b)->Integer.compare(a.val,b.val));
// Allocate the memory on the stack instead of the heap.ListNodedummy(0);ListNode*curr=nullptr;ListNode*prev=nullptr;ListNode*next=nullptr;ListNode*slow=nullptr;ListNode*fast=nullptr;ListNode*head=nullptr;ListNode*tail=nullptr;ListNode*l1=nullptr;ListNode*l2=nullptr;
// 2D Vectorconstintm=matrix.size();constintn=matrix[0].size();// If there're two strings.constintm=s1.length();constintn=s2.length();// If there's only a string.constintn=s.length();
1 2 3 4 5 6 7 8 910
// 2D Arrayfinalintm=matrix.length;finalintn=matrix[0].length;// If there're two strings.finalintm=s1.length();finalintn=s2.length();// If there's only a string.finalintn=s.length();
1 2 3 4 5 6 7 8 910
# 2D Listm=len(matrix)n=len(matrix[0])# If there're two strings.m=len(s1)n=len(s2)# If there's only a string.n=len(s)