Leetcode Revision
Leetcode Revision
ArrayDequeue in java:
→ This should be used instead of stack.
Queue in java:
Queue<String> queue = new LinkedList<>();
Methods: .add(), .offer(), .poll(), peek(), isEmpty()
Minheap in java:
.PriorityQueue is a priority heap implementation, and by default, it is a Min Heap. Here's an
example of how to create a Min Heap in Java:
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
Queue<Integer> heap = new PriorityQueue<>(
(n1, n2) -> hm.get(n1) - hm.get(n2));
String in java:
String str = new String();
→ operations: str.length(), str.charAt(i), str.contains(), str1.equals(str2) (compares two
strings), str.substring(start, end), str.compareTo(str1)
→ Use str.toCharArray() to convert string into character array.
→ String is immutable
StringBuilder in java:
StringBuilder sb = new StringBuilder();
→ It is mutable and is faster than string.
→ operations: sb.length(), sb.deleteCharAt(), sb.CharAt(), sb.toString(), str.substring(),
sb.append()
Arrays in java:
Int[] arr = new int [size];
Int[] myArray = new int[]{0, 1, 2, 3};
→ operations: arr.length, Arrays.sort(arr), Arrays.fill(arr, value)
→ int max = Arrays.stream(arr).max().getAsInt(); is used to find the maximum element in an
array. This method is slow so it is better to use for loop.
→ Math.max(a, b)is used to find max between two numbers.
→ Sorting a 2D array according to a given column_index
Arrays.sort(arr, (a,b) -> Integer.compare(a[col_index], b[col_index]);
List in java:
List<Obj> list = new ArrayList<Obj> ();
List<Integer> numbers = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6));
→ operations: .add(), .addAll(), .set(), .indexOf(), .remove(), .get(), .contains(), size(), clear()
→ add() method return boolean variable
Hashmap in java:
HashMap<Data_type, Data_type> map = new HashMap<>();
Methods: .put(key, value), putIfAbsent(key, value), .get(key), .isEmpty(), .size(),
.remove(key), .containsKey(key), .containsValue(value), .clear(), .getOrDefault(key,
default_value)
→ hashmap cannot contain duplicate keys. If you put a duplicate key, it will override the
existing key.
Hashset in java:
HashSet<Data_type, Data_type> hs= new HashSet<>();
Methods: .add()
This might not be true but from my experience, two function executions in a recursion
function act like a for loop
→ bit manipulation:
1. Bitwise AND(&)
2. Bitwise OR(|)
3. Bitwise XOR(^)
4. Left Shift(<<)
5. Right Shift(>>)
# XOR:
→ in xor, if no.of 1’s is odd =1, if no.of 1’s is even = 0
→ a XOR a = 0
→ a XOR 0 = a
→ ^ for cancelling out the same bits.
→ Cancels duplicacy.
→ Can be used to find single element among duplicates.
→ Given n, print the xor of all numbers between (1, N) (inclusive of N):
→ if (n % 4 == 0) print(n)
→ if (n % 4 == 1) print(1)
→ if (n % 4 == 2) print(n + 1)
→ if (n % 4 == 3) print(0)
Ex: swap two numbers, single number problem
# BITWISE AND:
→ if (n & 1 == 0)
even
Else
Odd
→ a & 1 = LSB of a
# LEFT SHIFT <<:
→ for a number n, n << i = n*(2**i)
# RIGHT SHIFT >>:
→ n >> i = n/(2**i)
# PROPERTIES:
→ (a+b) = (a|b) + (a&b)
→ (a+b) = (a^b) + 2*(a&b)
→ for a number n, i th bit can be calculated by => (n & 1 << i)
→ set the i th bit to 1 => n = (n | (1 << i))
→ clear the i th bit to 0 => n = (n & ~(1 << i))
→ remove the last set bit => n = (n & (n - 1))
→ check if number n is a power of 2 => (n & (n - 1)) == 0
→ the xor of of all subsets in an array is always 0
→ To generate all subsets of an array, power set algorithm is used. Can be used when the
problem wants to cover all combinations of an array.
→ bit masking