1. General Introduction
Before submitting your solution, it's recommended to test your code against various test cases, especially the sample ones provided in the problem statement.
The test case area is pre-populated with example test cases by default, but you can modify these cases to test different scenarios. The image below highlights the test case area in red.
Example: Let's use the problem Find the Sum of Subsequence Powers as a reference.
After setting up your test cases, click "Run" to execute them on LeetCode's server. This will display the results, helping you check the output of your code against these test cases. Once you've addressed any issues in your code, click the "Submit" button to thoroughly verify your solution, as this will run your code against all system test cases to ensure its correctness.
2. Input Modes
When entering test cases, you can choose between two input modes: Tab-Based or Raw. To switch between them, click on the </> Source
button located below the test case panel.
2.1 Tab-Based Input
This mode provides a structured and user-friendly interface. You can input parameters one by one using the graphical interface. For example, to input a test case with nums = [2, 3, 1, 4]
and k = 2
, simply enter these values in their respective fields.
2.2 Raw Input
In Raw Input mode, you enter parameters on separate lines. To test multiple cases, you can input them consecutively. Here's an example of how to input two test cases:
This method is particularly useful when you need to input or edit multiple test cases at once.
3. Understanding LeetCode Input Formats
Correctly inputting test cases in LeetCode is crucial for testing and debugging your code. This section guides you through the various input types you'll encounter, from basic data types to complex custom data structures and special input scenarios.
3.1 Basic Data Types
This section covers fundamental data types like integers, booleans, characters, and strings. These are the building blocks for more complex data structures, so mastering their input format is essential for handling advanced structures.
Type |
Description |
Examples |
Notes |
---|---|---|---|
integer
|
A numerical data type that represents whole numbers without any decimal component. Typically 4 bytes (32 bits) in size.
|
|
The range for an integer is [-231, 231-1].
|
long
|
An extended version of the integer type that can store larger numerical values. Typically 8 bytes (64 bits) in size.
|
|
The range for a long integer is [−263, 263-1].
|
boolean
|
A data type that represents one of two binary values:
true or false . |
|
|
double
|
A numerical data type used to represent floating-point numbers with decimal notation. Typically 8 bytes (64 bits) in size.
|
|
The range for a double is approximately [−1.7 × 10308, 1.7 × 10308].
Avoid using commas or multiple decimal points.
|
character
|
A data type that represents a single alphanumeric character, symbol, or punctuation mark. Typically 1 byte (8 bits) in size, supporting ASCII.
|
|
Character must be within the standard ASCII range (0 to 127).
A character must be enclosed in double quotes (
" ). Use escape sequences (e.g., \\ for backslash) where necessary. |
string
|
A sequence of characters, including letters, digits, punctuation, whitespace, and other symbols.
|
|
Strings must be enclosed in double quotes (
" ). Each character in the string must be within the standard ASCII range (0 to 127). |
3.2 Standard Data Structures
LeetCode problems often involve complex data structures like Linked Lists and Binary Trees, which are pre-defined to save you time. The table below shows how to format and input these structures for your test cases.
Type
|
Description
|
Examples
|
Notes
|
---|---|---|---|
Array
|
A data structure that holds a sequence of elements of the same data type.
|
|
Arrays must contain elements of the same data type.
|
2D Array
|
An array of arrays, where each sub-array represents a row. It can hold elements in a matrix-like structure.
|
|
2D arrays can either have a fixed number of columns (e.g.,
int[,] in C#) or a variable number of columns (e.g., int[][] in C#). Always check the problem's requirements or the programming language's specifics to understand the structure. |
Linked List
|
A linear data structure consisting of nodes where each node contains a value and a reference to the next node.
|
|
Serialized Format:
Note: Always use the provided |
Binary Tree
|
A tree data structure in which each node has at most two children, referred to as the left and right child.
|
|
Serialized Format:
Note: Always use the provided |
3.3 Custom Data Structures
Some LeetCode problems define custom types or data structures specific to the problem. The structure and input format are detailed in the problem description. The type or data structure definition, including its implemented class, constructor, and helper methods, is provided in the description and code comments.
Examples:
-
N-ary Tree
An extension of a Binary Tree where each node can have any number of children.
Sample Problems:
N-ary Tree Preorder Traversal
Serialize and Deserialize N-ary Tree (Premium) - Multi-level Doubly Linked List
A linked list where nodes can have child lists that need to be flattened.
Sample Problem: Flatten a Multilevel Doubly Linked List - Linked List with Random Pointers
A linked list with an additional pointer in each node that can point to any node in the list.
Sample Problem: Copy List with Random Pointer - Undirected Graph
An undirected graph where each node has a value and a list of neighbor nodes.
Sample Problem: Clone Graph
3.4 Handling Special Inputs
In some LeetCode problems, certain input parameters are used internally to construct types or data structures and may not be directly accessible in your code. Understanding these special inputs is crucial for properly setting up your test cases.
Examples:
-
Linked List Cycle
A linked list that contains a cycle. The additionalpos
parameter denotes the index of the node that the tail's next pointer connects to. Only thehead
is passed into your function;pos
is used to construct the cycle and is not accessible within your function.
-
Guess Number Higher or Lower
A guessing game where your function tries to guess a secret number using higher or lower hints. The additionalpick
parameter is the secret number and is not accessible by your function. -
Robot Room Cleaner (Premium)
A robot is controlled to clean a room with obstacles. The parametersroom
,row
andcol
represent the room's map and the robot's initial position, respectively. You receive only arobot
object, which you must control using its interface to clean the room. The robot's position is not directly known, and you must rely on its APIs to navigate. -
Guess the Word
In this problem, you need to guess a hidden word using a series of guesses. Each guess returns the number of matching letters with the hidden word. The hidden word itself is not accessible to your function; only the response from each guess is available.
3.5 Design Problem Formats
Design problems on LeetCode are unique because they often require you to implement an interface with specific constraints and requirements. These problems require a blend of algorithmic knowledge and software engineering principles.
The input format for design problems consists of two parts:
- Function Names: A list of function names to be called on the custom class.
- Function Arguments: A list of arguments corresponding to each function call.
Example:
Consider the problem Min Stack. The input format is as follows:
["MinStack","push","push","push","getMin","pop","top","getMin"]
[[],[-2],[0],[-3],[],[],[],[]]
This input corresponds to the following sequence of function calls:
MinStack minStack = new MinStack();
minStack.push(-2);
minStack.push(0);
minStack.push(-3);
minStack.getMin(); // returns -3
minStack.pop();
minStack.top(); // returns 0
minStack.getMin(); // returns -2
This format helps you visualize how the class and its methods are used in the test cases.
3.6 Database Problem Formats
For database problems, the input format consists of the content of predefined tables. These tables are populated with data, and its input format is explained in detail here.