String, StringBuilder, StringBuffer String is a unchangeable object(although it does not belong to the 8 primitive types). If we change the string, we rebuild a new object that a reference “points” to it. So for an an object that always changes, the speed will slow down (JVM GC). At this time, we use StringBuffer(not thread safe) or StringBuilder(thread … More Java – String
Initialize a series of class objects-Array: Class Dog: Dog dog1 = new dog(); dog2,dog3,… Use a for loop to initialize but this is hard to name all new objects. Use Array instead: Dog.
Divide and Conquer if(start == end) return Item(start) mid = start + (end – start) / 2 *Compared with directly (end + start) / 2, this can calculate bigger integers. left part : start ~ mid right part : mid + 1 ~ end recursion middle part: Consider if part of the result is … More Algorithm Study Note(1) Summarize
Input: map: map.put(key,value); set: set.add(object); list: list.add(object); Class type and Primitive type: Used in map ,list: int -> Integer, boolean -> Boolean, char -> Character Contains: set.contains(object); length: list: list.size(); sort: array: Arrays.sort(int);
Reverse a Linklist The basic idea of reversing a linked list is “take out items and put in a new list(actually, this is not a real new list but a new reference), let the next item point to the former one”.
Initialize before operate Let’s take a list as an example: a list must be firstly initialized using “new ArrayList<>()…” before we add anything into it, or will cause a NullPointerException. Remove something from a list Here is the example: ZigZagIterator2. When removing something from a list in a for/while loop, we have to check the … More Attention and tips- java
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
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)
Topological sorting is about DAG(Directed Acyclic Graph). The main idea of Kahn algorithm is to: Repeatedly find the start node(no income path, in-degree is 0) and print it. To find the start node, there are many ways. Given in-degree is the most convenient one. While sometimes we might need a map(hash map) to map the … More BFS in Topological Sorting(Kahn)
Heap vs Stack more information: http://www.journaldev.com/4098/java-heap-space-vs-stack-memory