pseudoyu

pseudoyu

Blockchain | Programming | Photography | Boyi
github
twitter
telegram
mastodon
bilibili
jike

Commonly Used Data Structures for LeetCode Practice (Java Edition)

Introduction#

Recently, I started practicing algorithm problems on LeetCode. The main purpose of solving algorithm problems for work is to improve problem-solving skills and coding abilities, rather than using complex data structures like in algorithm competitions. Therefore, there are not many commonly used data structures and operations. Mastering them can greatly improve the quality of your code. Here, I will summarize them for easy reference.

Data Structures#

Array []#

Initialization#

// Initialize an array of size 10 with default value 0
int[] nums = new int[10];

// Initialize a 2D boolean array
boolean[][] visited = new boolean[5][10];

Common Methods#

// Generally, a non-empty check is performed at the beginning of a function, and then elements are accessed using index
if (nums.length == 0) {
    return;
}

for (int i = 0; i < nums.length; i++) {
    // Access nums[i]
}

String#

Initialization#

String s1 = "hello world";

Accessing Strings#

// String does not support direct access to characters using []
char c = s1.charAt(2);

Modifying Strings#

// String does not support direct modification of strings, it needs to be converted to char[] type to make modifications
char[] chars = s1.toCharArray();
chars[1] = 'a';
String s2 = new String(chars);

Comparing Strings#

// Always use the equals method for comparison, do not use ==
if (s1.equals(s2)) {
    // Strings are equal
} else {
    // Strings are not equal
}

Concatenating Strings#

// Supports concatenation using +, but the efficiency is not high
String s3 = s1 + "!";

Efficiently Concatenating Strings using StringBuilder#

StringBuilder sb = new StringBuilder();

for (char c = 'a'; c <= 'f'; c++) {
    // The append method supports concatenating characters, strings, numbers, and other types
    sb.append(c);
    String result = sb.toString();
}

Dynamic Array ArrayList#

Initialization#

// Initialize a dynamic array to store String types
ArrayList<String> strings = new ArrayList<>();

// Initialize a dynamic array to store int types
ArrayList<Integer> nums = new ArrayList<>();

Common Methods#

// Check if the array is empty
boolean isEmpty()

// Return the number of elements
int size()

// Access an element at a specific index
E get(int index)

// Add an element to the end of the array
boolean add(E e)

Doubly Linked List LinkedList#

Initialization#

// Initialize a doubly linked list to store String types
LinkedList<String> strings = new LinkedList<>();

// Initialize a doubly linked list to store int types
LinkedList<Integer> nums = new LinkedList<>();

Common Methods#

// Check if the list is empty
boolean isEmpty()

// Return the number of elements
int size()

// Add an element to the end of the list
boolean add(E e)

// Remove the last element from the end of the list
E removeLast()

// Add an element to the beginning of the list
void addFirst(E e)

// Remove the first element from the beginning of the list
E removeFirst()

HashMap#

Initialization#

// Initialize a HashMap that maps integers to strings
HashMap<Integer, String> map = new HashMap<>();

// Initialize a HashMap that maps strings to arrays
HashMap<String, int[]> map = new HashMap<>();

Common Methods#

// Check if a key exists
boolean containsKey(Object key)

// Get the value corresponding to a key, returns null if it does not exist
V get(Object key)

// Get the value corresponding to a key, returns a default value if it does not exist
V getOrDefault(Object key, V defaultValue)

// Put a key-value pair into the HashMap
V put(K key, V value)

// Put a key-value pair into the HashMap, if it already exists, do nothing
V putIfAbsent(K key, V value)

// Remove a key-value pair and return the value
V remove(Object key)

// Get all keys in the HashMap
Set<K> keySet()

Queue#

Initialization#

// In Java, Queue is an interface
// Initialize a queue to store strings
Queue<String> q = new LinkedList<>();

Common Methods#

// Check if the queue is empty
boolean isEmpty()

// Return the number of elements
int size()

// Return the element at the front of the queue
E peek()

// Remove and return the element at the front of the queue
E poll()

// Insert an element at the end of the queue
boolean offer(E e)

Stack#

Initialization#

// Initialize a stack of type int
Stack<Integer> s = new Stack<>();

Common Methods#

// Check if the stack is empty
boolean isEmpty()

// Return the number of elements
int size()

// Push an element onto the top of the stack
E push(E e)

// Return the element at the top of the stack
E peek()

// Remove and return the element at the top of the stack
E pop()

Conclusion#

The journey of coding practice is long... Keep going!

References#

  1. LeetCode Official Website
  2. labuladong's Algorithm Notes
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.