DFS notes

When to use dfs? Generally speaking, there are several situations as follows: Combination Sample answer: combination sum2  Key Point: 1) sorting before dfs 2) use Index to help remove duplicates 3) consider out of boundary question whenever using arrays Use DFS without recursion: Use STACK(Every time pop an item, push the it’s children, thus accomplish DFS) Stack FILO … More DFS notes

Java Notes(2)

Collection, Collections Collection: interface(List :LinkedList, ArrayList; Vector: Stack; Set) Collections : a tool class e.g. Collections.sort(list) Collections.sort vs Arrays.sort Collections.sort deals with object that from collection interface(list,set,vector,stack), arrays deals with array object. e.g. Class temp contains an array and a list. Arrays.sort(temp.array) Collections.sort(temp.list) Comparator Default sorting in sort method uses merge sort. If we want … More Java Notes(2)


Initialize a graph in java Usually, we use Hashmap to store a graph in java. Because in hashmap/hashset, there won’t be a duplicate even if we repeat putting a same item in the map/set. Here is a template of initializing a hashmap graph:

Java Notes(1) String and Int

1.Initialization String a ; a must be initialized if further operations will be taken (unless assignment). eg. String a = null; a += “aaa”; System.out.println(a); This will print “nullaaa”. 2. Int to String to Int String a = Integer.toString(i); Int a = Integer.parseInt(a);  

Java Queue, Stack

FIFO: Queue  FILO: Stack Queue Since java 5 we can use queue by importing import java.util.Queue; Insert an item at the end of a queue and pop out from the head of the queue. Queue is an interface that there are several implementing classes, but the most used class is LinkedList Queue<TreeNode> queue = new … More Java Queue, Stack