Java frequently used data structures and methods for leetcode

Array

Common declarations:

int[] a = new int[100];
int[] b = {4, 1, 5};
int[] c = new int[] {1, 2, 3};

Two-dimensional arrays:

int[][] grid = {{1, 2}, {3, 4}};

int[][] jagged = new int[3][];
jagged[0] = new int[3];
jagged[1] = new int[2];
jagged[2] = new int[5];

Useful array operations:

  • arr.length is an array field, not a method.
  • Arrays.toString(arr) prints a one-dimensional array.
  • Arrays.stream(arr).sum() sums an int[].
  • Arrays.stream(arr).min().getAsInt() returns the minimum.
  • Arrays.fill(arr, -1) fills every slot with -1.
  • System.arraycopy(nums, 0, newNums, 1, n - 2) copies a range.
  • Arrays.copyOf(original, 5) returns a new array of length 5.
  • Arrays.copyOfRange(original, from, to) copies a half-open range.

Sorting:

Arrays.sort(intervals, (a, b) -> a[0] - b[0]);

Arrays.sort(points, (a, b) -> {
    if (a[1] == b[1]) return 0;
    if (a[1] < b[1]) return -1;
    return 1;
});

Array/list conversion:

String[] a = new String[] {"A", "B", "C", "D"};
List<String> list = Arrays.asList(a);

List<List<Integer>> rows = new ArrayList<>();
for (int[] row : res) {
    rows.add(Arrays.stream(row).boxed().collect(Collectors.toList()));
}

int[] arr = list.stream().mapToInt(i -> i).toArray();
List<Integer> boxed = Arrays.stream(arr).boxed().collect(Collectors.toList());

Convert a list of arrays back to a two-dimensional array:

LinkedList<int[]> merged = new LinkedList<>();
return merged.toArray(new int[merged.size()][]);

String

Common string methods:

  • s.substring(0, 3)
  • s.substring(begin)
  • s.equals(t)
  • s.length()
  • s.charAt(i)
  • s.indexOf(ch)
  • s.indexOf(ch, fromIndex)
  • s.indexOf(substring)
  • s.contains(sequence)
  • s.isEmpty()
  • s.replace("abc", "$")
  • s.equalsIgnoreCase(other)
  • String.valueOf(num)
  • s.toLowerCase()
  • s.toUpperCase()
  • s.trim()
  • s.split("+")

Do not use == to compare string contents. Use equals.

Loop over characters:

for (char c : str.toCharArray()) {
    // ...
}

boolean lower = Character.isLowerCase(word.charAt(i));

StringBuilder:

StringBuilder sb = new StringBuilder("abc");
sb.append(c);
String s = sb.toString();
sb.reverse();
sb.deleteCharAt(path.length() - 1);
sb.setCharAt(0, '$');
sb.insert(sb.length(), '$');

Formatting numbers:

DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(num));

String rounded = String.format("%.2f", num);
String padded = String.format("%02d", seatNum);

Collections

Base collection type:

Collection<E> p = new ArrayList<>();
Collection<E> q = new HashSet<>();

Frequently used methods:

MethodNotes
add(E element)Add one element.
addAll(collection)Add all elements from another collection.
clear()Remove all elements.
contains(element)Check membership.
containsAll(collection)Check whether all elements exist.
isEmpty()Check whether the collection is empty.
remove(element)Remove one matching element.
removeAll(collection)Remove all matching elements.
size()Number of elements.
toArray()Convert to an object array.

Sort with a comparator:

Comparator<Apple> byWeight = new Comparator<Apple>() {
    public int compare(Apple a1, Apple a2) {
        return a1.getWeight().compareTo(a2.getWeight());
    }
};

Comparator<Apple> byWeightLambda =
    (Apple a1, Apple a2) -> a1.getWeight().compareTo(a2.getWeight());

Collections.sort(values, Collections.reverseOrder());

Other helpers:

  • Collections.binarySearch(seats, requestedSeat)
  • Collections.reverse(seatList)
  • Collections.min(seatList)
  • Collections.max(seatList)
  • Collections.shuffle(seatList)
  • Collections.copy(dest, src)
  • Collections.swap(seatList, i, j)

ArrayList And Stack

ArrayList examples:

List<String> list = new ArrayList<>();
List<Integer> sub = nums.subList(start, endIndex);

list.size();
list.add(item);
list.get(index);
list.set(index, newItem);
list.remove(index);
list.contains(item);
list.indexOf(item);

Stack:

Stack<E> stack = new Stack<>();

stack.empty();
stack.push(item);
stack.pop();
stack.peek();
stack.search(item);

Deque

ArrayDeque and LinkedList are common implementations:

Deque<String> queue = new ArrayDeque<>();

Common methods:

  • peek()
  • peekFirst()
  • peekLast()
  • contains(item)
  • offer(item)
  • offerFirst(item)
  • offerLast(item)
  • poll()
  • pollFirst()
  • pollLast()
  • remove()
  • remove(index)
  • remove(object)
  • removeFirstOccurrence(object)
  • removeLastOccurrence(object)

Iterator:

Iterator<String> it = linkedList.iterator();
while (it.hasNext()) {
    it.next();
}

Set

Set declarations:

Set<String> hashSet = new HashSet<>();
Set<String> linkedHashSet = new LinkedHashSet<>();
Set<String> treeSet = new TreeSet<>();

Common set operations:

  • s1.containsAll(s2)
  • s1.addAll(s2)
  • s1.removeAll(s2)
  • s1.retainAll(s2)

If an object is used in a Set or as a Map key, override both equals and hashCode.

@Override
public boolean equals(Object obj) {
    // ...
}

@Override
public int hashCode() {
    // ...
}

Map

Map declarations:

Map<K, V> hashMap = new HashMap<>();
Map<K, V> linkedHashMap = new LinkedHashMap<>();
Map<K, V> treeMap = new TreeMap<>();

Common map methods:

  • put(key, value)
  • putAll(map)
  • putIfAbsent(key, value)
  • remove(key)
  • remove(key, value)
  • clear()
  • containsKey(key)
  • containsValue(value)
  • get(key)
  • getOrDefault(key, defaultValue)
  • isEmpty()
  • size()
  • keySet()
  • entrySet()

Iterate entries:

for (Map.Entry<String, String> entry : map.entrySet()) {
    System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}

Iterate keys:

for (String key : language.keySet()) {
    language.get(key);
}

Lambda iteration:

HashMap<Integer, Integer> map = new HashMap<>();
map.put(2, 3);
map.put(3, 4);

map.forEach((k, v) -> {
    System.out.println(k);
    System.out.println(v);
});

Build nested structures:

Map<Integer, Stack<Integer>> group = new HashMap<>();
group.computeIfAbsent(freq, key -> new Stack<>()).push(x);

Find all keys with the maximum value:

private List<Integer> keysWithMaxValue(Map<Integer, Long> counts) {
    if (counts.isEmpty()) {
        return Collections.emptyList();
    }

    long max = counts.values().stream().max(Comparator.naturalOrder()).get();
    return counts.entrySet().stream()
        .filter(entry -> entry.getValue() == max)
        .map(Map.Entry::getKey)
        .collect(Collectors.toList());
}

Get the maximum map value:

long max = dist.values().stream().max(Comparator.naturalOrder()).get();
Map.Entry<Integer, Long> maxEntry = map.entrySet().stream()
    .max(Map.Entry.comparingByValue())
    .get();

TreeMap And TreeSet

Reverse-order TreeMap:

TreeMap<Integer, Integer> scores = new TreeMap<>(Collections.reverseOrder());
TreeMap<Integer, Integer> scores2 = new TreeMap<>((a, b) -> b - a);

PriorityQueue

Min heap and max heap:

PriorityQueue<Integer> minHeap = new PriorityQueue<>();
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
PriorityQueue<Integer> heapWithValues = new PriorityQueue<>(Arrays.asList(3, 1, 2));

Custom comparator with arrays:

PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> {
    if (a[0] == b[0]) {
        return b[1] - a[1];
    }
    return b[0] - a[0];
});

int[] entry = new int[] {strength, i};
pq.add(entry);

Custom comparator with a class:

PriorityQueue<Pair> q = new PriorityQueue<>(
    (Pair a, Pair b) ->
        a.first == b.first ? b.second - a.second : b.first - a.first
);

class Pair {
    int first;
    int second;

    Pair(int first, int second) {
        this.first = first;
        this.second = second;
    }
}

Common priority-queue methods:

  • pq.add(item)
  • pq.offer(item)
  • pq.peek()
  • pq.poll()
  • pq.remove()

Example:

Queue<Candidate> pq = new PriorityQueue<>(
    (a, b) -> a.totalEstimate - b.totalEstimate
);
pq.add(new Candidate(0, 0, 1, estimate(0, 0, grid)));

Arrays.asList(int[]) does not produce List<Integer>. It produces a one-element List<int[]>. Use streams for primitive arrays:

int[] a = {1, 2, 3};
List<Integer> list = IntStream.of(a).boxed().collect(Collectors.toList());

Math And Integer

Math helpers:

  • Math.abs(a)
  • Math.max(a, b)
  • Math.sqrt(x)
  • Math.pow(a, b)
  • Math.ceil(a)
  • Math.floor(a)
  • Math.random()
  • Math.log(x)
  • Math.log10(x)
  • Math.sin(a)
  • Math.cos(a)
  • Math.tan(a)

Random numbers:

Random random = new Random();
int value = random.nextInt(n);
int inclusive = random.nextInt((max - min) + 1) + min;

Integer helpers:

  • Integer.parseInt(s)
  • Integer.valueOf(s)
  • Integer.toBinaryString(i)

Reference:

https://github.com/itsPrasheel/lastMinuteJavaSyntax

Related Posts

Leetcode: Array questions with Python

134.Gas StationThere are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You have a car with an unlimited gas tank and it costs cost[i] of gas to trave

read more

leetcode questions: Backtracking

Template def backtrack(candidate): if find_solution(candidate): output(candidate) return # iterate all possible candidates. for next_candidate in list_of_candid

read more

leetcode questions: BFS

BFS Template /** * Return the length of the shortest path between root and target node. */ int BFS(Node root, Node target) { Queue<Node> queue; // store all nodes which are waiting

read more

leetcode questions: Binary Search

bisect.bisect_left(a, x) https://dynalist.io/d/RWIGNj7DLlzkBed-3ZqhuBg_#z=cbr2Mkrig9KhE6Lxfwhm31IS O(log n) greater than or equal to the targeted value. If all elements are less than x, return `

read more

leetcode questions: Bit

common tricks x & (-x) to isolate/get the rightmost 1-bit This operation reverts all bits of x except the rightmost 1-bit. Hence, x and -x have just one bit in common - the rightmost 1-bit

read more

leetcode questions: DFS

DFS Templates Template 1: /* * Return true if there is a path from cur to target. */ boolean DFS(Node cur, Node target, Set<Node> visited) { return true if cur is target; for (

read more

leetcode questions: dynamic programming

Intro Good video: Dynamic Programming - Learn to Solve Algorithmic Problems & Coding Challenges - YouTube DP is a style of coding where you store t

read more

leetcode: mini spanning tree, single src shortest path, topological

Minimum spanning tree A minimum spanning tree is a spanning tree with the minimum possible total edge weight in a “weighted undirected graph”. [Min Cost to Connect All Points - LeetCode](https://

read more

Leetcode: LinkedList in Python

create a dummy nodeMerge two linked lists.# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = Nonecla

read more

Python list/dict functions for leetcode

Listlen(array) : length of array list.index(obj)enumerate(array) : adds counter at the beginning. for count, item in enumerate(['x','y','z']) # 0x 1y 2z`range(0,len(mylis

read more

Python String functions for leetcode

print(f'best RF score: {grid.best_score_:.3f}') str.index(sub[,start[,end]]) str.strip() remove spaces at the beginning and the end strip(str-not-want)lower() `print(str.upp

read more

leetcode: Trie

Basic A Trie is a special form of a Nary tree. Typically, a trie is used to store strings. Each Trie node represents a string (a prefix). Each node might have several children nodes while the pat

read more

Common design patterns in Java

Strategy The Strategy Pattern defines a family of algorithms, encapsulates each one, and makes them interchangeable. Strategy lets

read more