0% found this document useful (0 votes)
3 views4 pages

Leetcode Revision

The document provides an overview of various data structures and their operations in Java, including Stack, ArrayDeque, Queue, MinHeap, String, StringBuilder, Arrays, List, HashMap, and HashSet. It also covers string manipulation, conversion methods, and important algorithms such as binary search, dynamic programming, and bit manipulation techniques. Additionally, it highlights key differences in string comparison and offers insights into recursion and greedy algorithms.

Uploaded by

Kunal Bang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Leetcode Revision

The document provides an overview of various data structures and their operations in Java, including Stack, ArrayDeque, Queue, MinHeap, String, StringBuilder, Arrays, List, HashMap, and HashSet. It also covers string manipulation, conversion methods, and important algorithms such as binary search, dynamic programming, and bit manipulation techniques. Additionally, it highlights key differences in string comparison and offers insights into recursion and greedy algorithms.

Uploaded by

Kunal Bang
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Stack is java:

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


E is the type of object.
→ operations: push(), pop(), peek(), isEmpty()

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));

→ operations: minheap.add(value), minheap.poll()

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()

Uppercase methods and lower case methods:


→ Character.isUpperCase(ex), Character.toUpperCase(ex)
→ Character.isLowerCase(ex), Character.toLowerCase(ex)

How to convert String to int:


→ Use Integer.parseInt(string) or Integer.valueOf(string) to convert string to int.

How to convert char to String:


→ Use Character.toString(char)

Difference between .equals() and == in string comparison:


→ == checks if both objects point to the same memory location whereas .equals() evaluates
to the comparison of values in the objects.

This might not be true but from my experience, two function executions in a recursion
function act like a for loop

Some important functions:


→ Math.abs(), Math.max(arg1, arg2)
Leetcode Algorithms:
→ In problems where in some test cases the head of the linked list might need to be
changed. In such problems, in the beginning, create a new node and point its next element
to the head. Then return newNode.next as head. This newNode will allow work with the
linked list freely without worrying about the head.
→ simple sorting:
→ Major clue is when the order in the given array or data structure is not important.
→ binary search
-​ Used when Array is sorted
-​ When using binary search in solution set.
-​ Binary search for min(max) or max(min)
​ while(left <= right){
​ ….
​ If (arr[mid] <= var){
​ ​ Result = mid;
​ ​ Left = mid + 1;
} else {
​ Right = mid - 1;
}
}
→ Sliding window technique.
→ prefix sum
→ Interval problems (57, 452)
→ hash map, minheap problems
→ tortoise and hare algorithm - has applications in arrays also
→ recursion/backtracking (subset problems):
→ Used in problems where we have to find all combinations and filter out the useful ones.
→ Identify recurrence relation first.
→ Greedy algorithm
-​ Kadane’s algorithm
→ Dynamic programming
→ When you stand at an instance and have multiple decisions to make, this algorithm is
used.
Steps to approach dp problems:
Find recursive relation
Recursive (top-down)
Recursive + memo (top-down)
Iterative + memo (bottom-up)
Iterative + N variables (bottom-up)

→ 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy