min-heap
default in java
// V1 // - This creates a min-heap using the natural ordering of
Integer objects (i.e., integers will be ordered in ascending order by
default). // - No custom comparator is provided, so Java uses the
default comparison of integers, which is already based on their
numerical value. PriorityQueue
// V2 // - This also creates a min-heap, but it uses a custom
comparator. PriorityQueue
- Max-heap
- LC 1046
```java
// java
// V1
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Comperator.reverseOrder());
// V2
PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
int diff = o2 - o1;
return diff;
}
});
charAt()
offer a method a access String element with
idx// java
String s = "www.google.com";
char result = s.charAt(6);
System.out.println(result);
// LC 647
// ...
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
--;
l++;
r++;
ans}
// ...
Conclusion:
Arrays.asList
: only wrap existing array,
add
, remove
methods (but has modify method)
are NOT implemented
new ArrayList
: implement “add”, “remove” and
“modify” methods, not affect original array ->
preferable
// java
// LC 973
/** NOTE !!!
*
* below init an array with (k length, 2 width) (aka k x 2 dimension)
*
*/
int[][] ans = new int[k][2];
int k = 4;
int[][] x = new int[k][2];
System.out.println(">>> before " + Arrays.deepToString(x));
[0] = new int[]{0,1};
x[1] = new int[]{0,2};
x
System.out.println(">>> after " + Arrays.deepToString(x));
int[] x2 = new int[]{1, 2, 3};
System.out.println(Arrays.toString(x2)); // Output: [1, 2, 3]
// how to print context in 2D array ?
System.out.println(">>> before " + Arrays.deepToString(x));
// java
// LC 57
/**
*
* 1) Array -> List
*
*/
Integer [] arr1 = new Integer[]{1,2,3};
/** Arrays.asList */
List<Integer> list1 = Arrays.asList(arr1); // NOTE here !!!
/**
*
* 2) List -> Array
*
*/
List<Integer> list2 = new ArrayList<>();
.add(1);
list2.add(2);
list2.add(3);
list2
/**
* NOTE !!! `toArray`
*
* list2.toArray with size
*
*/
Integer [] arr2 = list2.toArray(new Integer[list2.size()]); // NOTE here !!!
// java
// LC 399
HashMap<String, HashMap<String, Double>> gr = new HashMap<>();
for (int i = 0; i < equations.size(); i++) {
String dividend = equations.get(i).get(0);
String divisor = equations.get(i).get(1);
double value = values[i];
.putIfAbsent(dividend, new HashMap<>());
gr.putIfAbsent(divisor, new HashMap<>());
gr
.get(dividend).put(divisor, value);
gr.get(divisor).put(dividend, 1.0 / value);
gr}
HashMap<String, HashMap<String, Double>>
)
to store keyA - valB - resC
info// java
// LC 844
// V1
String s = "abc";
for (char c: S.toCharArray()) {
// do sth
System.out.println("c = " + c);
}
// V2
// LC 49
String strs = "scvsdacvdsa";
char[] array = strs.toCharArray();
// or, can use myStr1.split(""), can access element in string as well
// Java
// Array VS List
// List
List<String> _list = new ArrayList<>(); // this is List (with "List" keyword)
// Array
int[] _array = {0,1,2,3}; // this is Array (without "List" keyword)
// java
// LC 102
public static void main(String[] args) {
List<Integer> tmpArray = new ArrayList<>();
System.out.println(tmpArray);
.add(1);
tmpArray.add(2);
tmpArraySystem.out.println(tmpArray);
System.out.println("--->");
List<List<Integer>> res = new ArrayList<>();
System.out.println(res);
.add(tmpArray);
resSystem.out.println(res);
}
// init a list with 2D content
// LC 406
// example 1
List<List<Integer>> commonCells = new ArrayList<>();
// example 2
List<int[]> result = new ArrayList<>(); //return value
Reverse
loop
over a list// java
List<Integer> list = new ArrayList<>();
.add(1);
list.add(2);
list.add(3);
list
/**
* NOTE !!!
*
* for reverse loop,
*
* we start from size - 1,
* end at >= 0
*
*/
for(int i = list.size() - 1; i >= 0; i--){
System.out.println(list.get(i));
}
// java
// LC 417
List<List<Integer>> commonCells = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
if (pacificReachable[i][j] && atlanticReachable[i][j]) {
// NOTE code here
.add(Arrays.asList(i, j));
commonCells}
}
}
index
// java
// LC 102
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> levels = new ArrayList<List<Integer>>();
// ...
while ( !queue.isEmpty() ) {
// ...
for(int i = 0; i < level_length; ++i) {
// fulfill the current level
// NOTE !!! this trick
.get(level).add(node.val);
levels// ...
}
// ...
}
// ..
}
// java
// ...
/**
* NOTE !!!
*
* via below, we can retrieve List val by idx,
* append new val to the existing array (with same idx)
*
*
* code breakdown:
*
* • res is a List<List<Integer>>, where each inner list represents a level of the tree.
* • res.get(depth) retrieves the list at the given depth.
* • .add(curRoot.val) adds the current node's value to the corresponding depth level.
*
*/
List<List<Integer>> res = new ArrayList<>();
// insert curRoot.val to current val in list at `depth` index
// NOTE !!! below
.get(depth).add(curRoot.val);
res
// ...
// java
// LC 107
// ...
List<List<Integer>> levels = new ArrayList<List<Integer>>();
Collections.reverse(levels);
// NOTE : reverse != decreasing order
// ...
List<Integer> list2 = new ArrayList<>();
.add(1);
list2.add(2);
list2.add(3);
list2
System.out.println("list2 = " + list2); // list2 = [1, 2, 3]
/** Reverse List
*
* // NOTE !!! reverse in place, e.g. NO return val
*/
Collections.reverse(list2);
System.out.println("list2 = " + list2); // list2 = [3, 2, 1]
// java (via StringBuilder)
// LC 567
private String reverseString(String input){
if (input.equals(null) || input.length() == 0){
return null;
}
StringBuilder builder = new StringBuilder(input).reverse();
return builder.toString();
}
// java
// LC 127
String x = "abcd";
/**
* 1st idx start from 0
* 2nd ind start from 1
*
* -> e.g. [1st_idx, 2nd_idx]
*
*/
System.out.println(x.substring(0,1)); // a
System.out.println(x.substring(0,2)); // ab
System.out.println(x.substring(2,3)); // c
System.out.println(x.substring(2,4)); // cd
//System.out.println(x.substring(2,10)); // `StringIndexOutOfBoundsException`
index = k
element at String// java
// LC 127
String y = "apple";
List<String> replaces = new ArrayList<>();
.add("1");
replaces.add("2");
replaces.add("3");
replaces.add("4");
replaces.add("5");
replaces
for(int i = 0; i < y.length(); i++){
String y_ = y.substring(0,i) + replaces.get(i) + y.substring(i+1, y.length());
System.out.println("y_ = " + y_);
/**
* result:
*
* y_ = 1pple
* y_ = a2ple
* y_ = ap3le
* y_ = app4e
* y_ = appl5
*
*/
}
// java
// LC 49
/** NOTE !!!
*
* We sort String via below op
*
* step 1) string to char array
* step 2) sort char array via "Arrays.sort"
* step 3) char array to string (String x_str = new String(x_array))
*
*/
String x = "cba";
char[] x_array = x.toCharArray();
Arrays.sort(x_array);
String x_str = new String(x_array);
// java (via .split(""))
String word = "heloooo 123 111";
for (String x : word.split("")){
System.out.println(x);
}
// LC 208
// ..
for (String c : word.split("")) {
= cur.children.get(c);
cur if (cur == null) {
return false;
}
}
// ..
str A
can be formed by the oter
str B
by deleting some of characters// LC 524
private boolean canForm(String x, String s){
/**
* NOTE !!!
*
* via below algorithm, we can check
* if "s" can be formed by the other str
* by some element deletion
*
* e.g.:
*
* check if "apple" can be formed by "applezz"
*
* NOTE !!!
*
* "i" as idx for s
* "j" as idx for x (str in dict)
*
* we go thorough element in "s",
* plus, we also check condition : i < s.length() && j < x.length()
* and once looping is completed
* then we check if j == x.length(),
* since ONLY when idx (j) reach
*
*
*/
int j = 0;
// V1 (below 2 approaches are both OK)
for (int i = 0; i < s.length() && j < x.length(); i++){
// NOTE !!! if element are the same, then we move x idx (j)
if (x.charAt(j) == s.charAt(i))
++;
j}
// V2
// for (int i = 0; i < y.length(); i++){
// if (j >= x.length()){
// return j == x.length();
// }
// if (x.charAt(j) == y.charAt(i))
// j++;
// }
/** NOTE !!!
*
* if j == x.length()
* -> means s idx can go through,
* -> means s can be formed by x (str in dict)
*/
return j == x.length();
}
// java
// LC 767
// access element in sb vis `sb.charAt[idx]`
// ...
StringBuilder sb = new StringBuilder("#");
/** NOTE !!! below */
if (currentChar != sb.charAt(sb.length() - 1)) {
// ...
}
// ...
// java
// LC 127
// modify val with idx
// ...
/** NOTE !!! below */
StringBuilder sb = new StringBuilder(word);
/** NOTE !!! StringBuilder can update val per idx */
.setCharAt(idx, c); // modify val to c per idx
sb
// ...
// java
// LC 345
// https://leetcode.com/problems/reverse-vowels-of-a-string/description/
void swap(char[] chars, int x, int y) {
char temp = chars[x];
[x] = chars[y];
chars[y] = temp;
chars}
// java
// LC 347
// NOTE !! we define size when init int[]
int[] top = new int[k];
for(int i = k - 1; i >= 0; --i) {
// assign val to int[] via below
[i] = heap.poll();
top}
M x N
boolean matrix// java
// LC 695
// LC 200
public static void main(String[] args) {
/**
* NOTE !!!
*
* if `boolean[][]`, the default val is `false`
* if `Boolean[][]`, the default val is `null`
*/
// ex1
Boolean[][] x = new Boolean[3][4];
System.out.println(x);
System.out.println(x[0][0]); // null
// ex2
boolean[][] y = new boolean[3][4];
System.out.println(y);
System.out.println(y[0][0]); // false
// ex3
boolean[][] seen;
= new boolean[3][4];
seen
int x = 3;
int y = 4;
boolean visit = boolean[y][x];
}
M x N
boolean matrix// java
// LC 130
// ...
int l = board.length;
int w = board[0].length;
for(int i = 0; i < l; i++){
for(int j = 0; j < w; j++){
// NOTE !!! below
/**
* NOTE !!!
*
* board[y][x],
*
* so the first is Y-coordination
* and the second is X-coordination
*
*/
if(board[i][j] == k){
// do sth
}
}
}
// ...
// java
// LC 345
// https://leetcode.com/problems/reverse-vowels-of-a-string/description/
// string -> char array
String s ="abcd";
char[] list = s.toCharArray();
System.out.println(list);
// char array -> string
char[] y = list;
String.valueOf(list);
// java
// V1
// https://youtu.be/xOppee_iSvo?t=206
Integer[] data = {5, 5, 7, 8, 9, 0};
Arrays.toString(data);
// V2
// LC 49
char array [] = strs.toCharArray();
Arrays.sort(array);
String.valueOf(array);
// java
// LC 844
// https://leetcode.com/problems/backspace-string-compare/editorial/
Stack<Character> ans = new Stack();
.push("a");
ans.push("b");
ans.push("c");
ans
String.valueOf(ans);
Arrays.sort
)// 1) Sort integer Array
// LC 252, LC 452
/// https://leetcode.com/problems/meeting-rooms/editorial/
int[][] intervals = new int[][]{ {15,20}, {0,30}, {5,10} };
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
// 2) Sort with 1 attr first, then sort on the other attr
// LC 406
// V1
Arrays.sort(people, (a, b) -> {
int x = Integer.compare(b[0], a[0]);
if(x == 0) return Integer.compare(a[1], b[1]);
else return x; });
// V2
Arrays.sort(people, new Comparator<int[]>() {
@Override
public int compare(int[] o1, int[] o2) {
// if the heights are equal, compare k-values
return o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0];
}
});
// java
// LC 452
Arrays.sort(points, (a, b) -> Integer.compare(a[0], b[0]));
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]))
:original
array in place directly (no new
array created)in place
.original
array intervals is
modified and sorted accordingly.Arrays.stream(intervals).sorted()
:NOT
modify the original
array, but create
the other sorted
arrayNOT
modify the original
array intervals in place.// java
// example code :
int[][] intervals = new int[][]{ {15,20}, {0,30}, {5,10} };
System.out.println("---> intervals before sorting");
for (int[] interval : intervals){
System.out.println("1st = " + interval[0] + ", 2nd = " + interval[1]);
}
// Using Arrays.stream(intervals).sorted();
Arrays.stream(intervals).sorted();
System.out.println("---> intervals after Arrays.stream(intervals).sorted()");
for (int[] interval : intervals){
System.out.println("1st = " + interval[0] + ", 2nd = " + interval[1]);
}
// Using Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]));
System.out.println("---> intervals after Arrays.sort(intervals, (a, b) -> Integer.compare(a[0], b[0]))");
for (int[] interval : intervals){
System.out.println("1st = " + interval[0] + ", 2nd = " + interval[1]);
}
Arrays.sort
array
sorting (Primitive Types 基本數據類型)Collections.sort
(Reference Types 引用類型)
list, HashMap
.. sorting// java
// LC 214
// https://stackoverflow.com/questions/1694751/java-array-sort-descending
/* -------------------------- */
/** Sort Array */
/* -------------------------- */
Integer[] _array = {5, 5, 7, 8, 9, 0};
// V1
Arrays.sort(_array, Collections.reverseOrder());
// V2
Arrays.sort(_array, (a,b) -> Integer.compare(-a, -b));
/* -------------------------- */
/** Sort List */
/* -------------------------- */
// V1
List<Integer> _list = new ArrayList();
Collections.sort(_list, Collections.reverseOrder());
// V2
Collections.sort(_list, (a,b) -> Integer.compare(-a, -b));
// V3
/** Using the Stream API:
(), sorted(), and collect(Collectors.toList()) to sort the list.
You can use stream*/
List<Integer> _list2 = new ArrayList<>(Arrays.asList(3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5));
// System.out.println(_list2);
= _list2.stream()
_list2 .sorted((a, b) -> b - a) // Using Comparator to sort in reverse order
.collect(Collectors.toList());
// System.out.println(_list2);
// V3
// LC 731
List<Integer[]> statusList;
.sort((x, y) -> {
statusListif (!x[0].equals(y[0])) {
return x[0] - y[0];
}
return x[1] - y[1]; // start (+1) comes before end (-1)
});
// example
Integer[] _array = new Integer[4];
[0] = 10;
_array[1] = -2;
_array[2] = 0;
_array[3] = 99;
_arrayArrays.stream(_array).forEach(System.out::println);
// Array sort
Arrays.sort(_array, Collections.reverseOrder());
System.out.println("---");
Arrays.stream(_array).forEach(System.out::println);
System.out.println("--->");
List<Integer> _list = Arrays.asList(1,2,3,4);
.forEach(System.out::println);
_list// List sort
Collections.sort(_list, Collections.reverseOrder());
System.out.println("---");
.forEach(System.out::println); _list
// java
// LC 524
/** NOTE !!!!
*
* custom sorting list
* via Collections.sort and new Comparator<String>()
*/
Collections.sort(collected, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
/**
* // First compare by length
* // NOTE !! inverse order, e.g. longest str at first
*/
int lengthComparison = Integer.compare(o2.length(), o1.length());
/**
* // If lengths are equal, compare lexicographically
* // NOTE !!! if lengths are the same, we compare lexicographically
*/
if (lengthComparison == 0) {
return o1.compareTo(o2); // lexicographical order
}
return lengthComparison; // sort by length
}
});
Hash Map's key and value
*****// LC 692
// IDEA: map sorting
HashMap<String, Integer> freq = new HashMap<>();
for (int i = 0; i < words.length; i++) {
.put(words[i], freq.getOrDefault(words[i], 0) + 1);
freq}
List<String> res = new ArrayList(freq.keySet());
/**
* NOTE !!!
*
* we directly sort over map's keySet
* (with the data val, key that read from map)
*
*
* example:
*
* Collections.sort(res,
* (w1, w2) -> freq.get(w1).equals(freq.get(w2)) ? w1.compareTo(w2) : freq.get(w2) - freq.get(w1));
*/
Collections.sort(res, (x, y) -> {
int valDiff = freq.get(y) - freq.get(x); // sort on `value` bigger number first (decreasing order)
if (valDiff == 0){
// Sort on `key ` with `lexicographically` order (increasing order)
//return y.length() - x.length(); // ?
return x.compareTo(y);
}
return valDiff;
});
Arrays.copyOfRange
: Get sub array// java
// LC 976
// https://leetcode.com/problems/largest-perimeter-triangle/description/
= [1,2,1,10, 11, 22, 33]
nums int i = 2;
int[] tmp = Arrays.copyOfRange(nums, i, i+3);
Arrays.toString(array)
: Print array
value// java
// LC 997
/**
* NOTE !!!
*
* via `Arrays.toString()`,
* we can print arrays value
*
* -> how to remember ?
*
* -> int[] is `array`
* -> and `Arrays` is the array Util in java
* -> so it has toString method()
*
*
*/
// ...
int[] toTrust = new int[n + 1];
int[] trusted = new int[n + 1];
System.out.println(">>> toTrust = " + Arrays.toString(toTrust));
System.out.println(">>> trusted = " + Arrays.toString(trusted));
// ...
// java
// LC 739
Stack<int[]> stack = new Stack<>();
int[] init = new int[2];
[0] = temperatures[0];
init[1] = 0;
init.push(init); stack
StringBuilder
)// LC 22
StringBuilder b = new StringBuilder("wefew");
System.out.println(b.toString());
.deleteCharAt(2);
bSystem.out.println(b.toString());
TreeMap
)// java
// LC 853
// V1
HashMap<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < position.length; i++){
int p = -1 * position[i]; // for inverse sorting
int s = speed[i];
.put(p, s);
map}
// order by map key
Map<Integer, Integer> tree_map = new TreeMap(map);
// java
// LC 853
// order Map key instead
HashMap<Integer, Integer> map = new HashMap<>();
Arrays.sort(map.keySet().toArray());
// java
// LC 346
// sort array in descending order
Arrays.sort(tmp, (x, y) -> Integer.compare(-x[1], -y[1]));
floorEntry
method in TreeMap
// floorEntry
// LC 1146
// ...
TreeMap<Integer, Integer>[] historyRecords;
// ...
public int get(int index, int snapId) {
return historyRecords[index].floorEntry(snapId).getValue();
}
// ...
// java
// (GPT)
// Create a HashMap
HashMap<String, Integer> map = new HashMap<>();
.put("apple", 5);
map.put("banana", 2);
map.put("cherry", 8);
map.put("date", 1);
map
// Print the original map
System.out.println("Original map: " + map);
// Sort the map by values
List<Map.Entry<String, Integer>> list = new ArrayList<>(map.entrySet());
.sort(Map.Entry.comparingByValue());
list
// Create a new LinkedHashMap to preserve the order of the sorted entries
LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
// // V1 : via Entry
// for (Map.Entry<String, Integer> entry : list) {
// sortedMap.put(entry.getKey(), entry.getValue());
// }
// V2 : via key
// NOTE !!! below
// Get the list of keys
List<String> keys = new ArrayList<>(map.keySet());
// NOTE !!! below
// Sort the keys based on their corresponding values
.sort((k1, k2) -> map.get(k1).compareTo(map.get(k2)));
keys
/**
* You can modify the code to avoid using Map.Entry by converting the
* HashMap to a list of keys and then sorting the keys based on
* their corresponding values. Here is the modified version:
*/
for (String key : keys) {
.put(key, map.get(key));
sortedMap}
// Print the sorted map
System.out.println("Sorted map: " + sortedMap);
key, value
on the same time// java
// LC 501
List<Integer> modes = new ArrayList<>();
/**
* NOTE !!! we use `Entry`
* to access both map's key and value
*/
for (Map.Entry<Integer, Integer> entry : node_cnt.entrySet()) {
if (entry.getValue() == maxFreq) {
.add(entry.getKey());
modes}
}
// java
// LC 875
// https://stackoverflow.com/questions/1484347/finding-the-max-min-value-in-an-array-of-primitives-using-java
int[] piles = new int[5];
int r = Arrays.stream(piles).max().getAsInt();
// java
// LC 355
// https://leetcode.com/problems/design-twitter/solutions/2720611/java-simple-hashmap-stack/
// https://blog.csdn.net/neweastsun/article/details/80294811
// https://blog.51cto.com/u_5650011/5386895
/**
*
*
*
*/
// init
<Integer, String> p1 = new Pair<>(1, "one");
Pair// get key
Integer k1 = p1.getKey();
// get value
String v1 = p1.getValue();
// use with other data structure
Queue<Pair<Integer, String>> q = new LinkedList<>();
.add(p1); q
// java
public class MyPair<U, V> {
public U first;
public V second;
MyPair(U first, V second){
this.first = first;
this.second = second;
}
// getter
// setter
}
// java
// LC 78
/** NOTE HERE !!!
*
* ++i : i+1 first, then do op
* i++ : do op first, then i+1
*
*/
// java
// LC 46
List<List<Integer>> ans = new ArrayList<>();
List<Integer> cur = new ArrayList<>();
//...
.add(new ArrayList<>(cur));
ans//...
// java
// LC 131
boolean isPalindrome(String s, int low, int high) {
while (low < high) {
if (s.charAt(low++) != s.charAt(high--)) return false;
}
return true;
}
// java
// LC 417
public int[][] DIRECTIONS = new int[][]{ {0, 1}, {1, 0}, {-1, 0}, {0, -1} };
// java
// LC 300
/** NOTE !!! ONLY work for 1 D (since array is 1 dimension) */
int[] dp = new int[10];
// fill op
Arrays.fill(dp,1);
// Small PQ (default min-heap)
PriorityQueue<Integer> smallPQ = new PriorityQueue<>();
// Big PQ (max-heap)
PriorityQueue<Integer> bigPQ = new PriorityQueue<>(Comparator.reverseOrder());
// Add elements to PQs
.add(5);
smallPQ.add(10);
smallPQ.add(1);
smallPQ
.add(5);
bigPQ.add(10);
bigPQ.add(1);
bigPQ
// Print elements from PQs
// small PQ
System.out.println("Small PQ (min-heap):");
while (!smallPQ.isEmpty()) {
System.out.println(smallPQ.poll());
}
// big PQ
System.out.println("Big PQ (max-heap):");
while (!bigPQ.isEmpty()) {
System.out.println(bigPQ.poll());
}
// java
// LC 347
// ...
// Step 1. Count the frequency of each element
Map<Integer, Integer> countMap = new HashMap<>();
for (int num : nums) {
.put(num, countMap.getOrDefault(num, 0) + 1);
countMap}
/** NOTE !!! how to init PQ below */
// Step 2. Use a Min-Heap (Priority Queue) to keep track of top K elements
PriorityQueue<Map.Entry<Integer, Integer>> heap = new PriorityQueue<>(
(a, b) -> a.getValue() - b.getValue()
);
// ...
add()
VS
offer()
in Queueadd(E e)
Method
full
,
add() throws an exception
(IllegalStateException).unlimited
elements (like
LinkedList), add() behaves similarly to `offer()`` and does not throw an
exception.offer(E e)
Method
full
,
offer() returns false instead of throwing an exception
.remove
method// java
// In Java, the remove() method is commonly used with various types of collections such as Queue, List, and Set. When used with a Queue, the remove() method is used to remove and return the front element of the queue.
/*
boolean remove(Object o);
- Purpose:
- Removes the first occurrence of the specified element from the queue. If the element exists in the queue, it is removed. If it doesn't exist, the queue remains unchanged.
- Return Type:
- Returns true if the element was successfully removed.
- Returns false if the element was not found in the queue (i.e., the queue remains unchanged).
- Throws:
- It may throw a NullPointerException if you pass null as an argument and the queue does not permit null elements (this depends on the specific implementation of Queue).
*/
// Create a Queue (LinkedList implements Queue)
Queue<Integer> queue = new LinkedList<>();
// Add elements to the Queue
.add(10);
queue.add(20);
queue.add(30);
queue.add(7); // Adding element 7
queue.add(40);
queue
System.out.println("Original queue: " + queue);
// Remove element 7 from the queue
boolean removed = queue.remove(7); // Removes the first occurrence of 7
// Print the result of removal
System.out.println("Was element 7 removed? " + removed); // true
// Print the modified queue
System.out.println("Queue after removal of 7: " + queue);
// Try to remove element 7 again
= queue.remove(7); // Element 7 no longer exists in the queue
removed
// Print the result of trying to remove 7 again
System.out.println("Was element 7 removed again? " + removed); // false
System.out.println("queue: " + queue); // queue: [10, 20, 30, 40]
.remove();
queueSystem.out.println("queue: " + queue); // queue: [20, 30, 40]
// LC 424
// NOTE : map.getOrDefault(key,0) syntax : if can find key, return its value, else, return default 0
.put(key, map.getOrDefault(key,0)+1);
map
// e.g.
.getOrDefault(key,0) map
// java
// LC 131
// ...
public List<List<String>> partition_1(String s) {
/** NOTE :
*
* we can init result, pass it to method, modify it, and return as ans
*/
List<List<String>> result = new ArrayList<List<String>>();
dfs_1(0, result, new ArrayList<String>(), s);
return result;
}
// ...
void dfs_1(int start, List<List<String>> result, List<String> currentList, String s) {
// ..
}
// java
// LC 152
= Math.max(Math.max(max * nums[i], min * nums[i]), nums[i]);
max = Math.min(Math.min(temp * nums[i], min * nums[i]), nums[i]); min
// java
// LC 98
// ...
/**
* NOTE !!!
*
* Use long to handle edge cases for Integer.MIN_VALUE and Integer.MAX_VALUE
* -> use long to handle overflow issue (NOT use int type)
*/
long smallest_val = Long.MIN_VALUE;
long biggest_val = Long.MAX_VALUE;
return check_(root, smallest_val, biggest_val);
// ...
// java
/**
* Integer.bitCount
*
* -> java default get number of "1" from binary representation of a 10 based integer
*
* -> e.g.
* Integer.bitCount(0) = 0
* Integer.bitCount(1) = 1
* Integer.bitCount(2) = 1
* Integer.bitCount(3) = 2
*
* Ref
* - https://blog.csdn.net/weixin_42092787/article/details/106607426
*/
// LC 338
https://stackoverflow.com/questions/4626812/how-do-i-instantiate-a-queue-object-in-java
A Queue is an interface
, which means you
cannot
construct a Queue directly.
Consinder use one of below implementation:
AbstractQueue, ArrayBlockingQueue, ArrayDeque, ConcurrentLinkedQueue, DelayQueue, LinkedBlockingQueue, LinkedList, PriorityBlockingQueue, PriorityQueue, or SynchronousQueue.
// java
// LC 742
/** NOTE
*
* Map.Entry<TreeNode, List<TreeNode>> entry : g.entrySet()
*
*/
Map<TreeNode, List<TreeNode>> g;
for (Map.Entry<TreeNode, List<TreeNode>> entry : g.entrySet()) {
if (entry.getKey() != null && entry.getKey().val == k) {
.offer(entry.getKey());
qbreak;
}
}
// ...
random.nextInt
// java
// LC 528
/** bound : range of random int can be returned */
// @param bound the upper bound (exclusive). Must be positive.
Random random = new Random();
// * @param bound the upper bound (exclusive). Must be positive.
System.out.println(random.nextInt(10));
System.out.println(random.nextInt(10));
System.out.println(random.nextInt(100));
// java
// LC 767
// ...
// Step 1: Count the frequency of each character
Map<Character, Integer> charCountMap = new HashMap<>();
for (char c : S.toCharArray()) {
.put(c, charCountMap.getOrDefault(c, 0) + 1);
charCountMap}
// Step 2: Use a priority queue (max heap) to keep characters sorted by
// frequency
/** NOTE !!!
*
* we use PQ to track the characters count sorted in order
*/
PriorityQueue<Map.Entry<Character, Integer>> maxHeap = new PriorityQueue<>(
(a, b) -> b.getValue() - a.getValue());
.addAll(charCountMap.entrySet());
maxHeap
// ...
// java
// LC 695
/**
* NOTE !!!!
*
* DON'T pass `area` as parameter to the DFS func (getBiggestArea)
*
* Reason:
*
* 1) in java, primitives pass by value.
* `int` is one of the primitives
*
* 2) meaning when we pass `area` to dfs,
* it actually sent as a new COPY everytime,
* which makes us CAN'T track/persist the new area value
*/
// ...
private int _getArea(int[][] grid, boolean[][] seen, int x, int y){
// ...
return 1 + _getArea(grid, seen, x+1, y) +
_getArea(grid, seen, x-1, y) +
_getArea(grid, seen, x, y+1) +
_getArea(grid, seen, x, y-1);
}
A classic Java trick for mapping characters to array
indices — especially when working with the lowercase English alphabet
('a'
to 'z'
).
[order.charAt(i) - 'a'] = i; orderMap
Let’s say:
= "hlabcdefgijkmnopqrstuvwxyz"; order
So order.charAt(i)
gets a character from the alien
alphabet — for example:
i = 0
: order.charAt(0)
=
'h'
'h' - 'a'
→ this subtracts ASCII/Unicode values:'h' = 104
, 'a' = 97
104 - 97 = 7
So:
[7] = 0; orderMap
This tells us: > In the alien alphabet, 'h'
is at
position 0
.
'c' - 'a'
?Character | ASCII | 'char' - 'a' |
---|---|---|
'a' |
97 | 0 |
'b' |
98 | 1 |
'c' |
99 | 2 |
'z' |
122 | 25 |
So 'c' - 'a' == 2'
— we use this to convert
'c'
to the index 2
.
Because we can now use a simple array of size 26:
int[] orderMap = new int[26];
And store the rank of each character in constant time using:
[c - 'a'] orderMap
Which is way faster than repeatedly doing:
.indexOf(c) // O(n) each time order
.charAt(i) - 'a' order
✔️ Converts a letter to a 0-based index
(e.g. 'a'
→ 0, 'z'
→ 25)
✔️ Works because characters are basically integers in Java
(char
uses UTF-16)
✔️ Super efficient for problems limited to lowercase letters
abc...z
char// LC 127
for (char c = 'a'; c <= 'z'; c++) {
System.out.println(c);
// output as below:
/** a b c d e f g h i j k l m n o p q r s t u v w x y z */
}