leetcode
- StringBuilder has no “sb.remove()” method, try these:
1 | sb.deleteCharAt(sb.length() - 1); |
- new boolean[] vs new Boolean[]
1 | boolean[] b1 = new boolean[1]; |
comparator
1
2
3
4
5
6
7
8
9
10Compartor<String> myComp = new Comparator<String>(){
public int compare(String s1, String s2){
return -1;
return 0;
return 1;
}
};
Arrays.sort(logs, myComp);
PriorityQueue
1
PriorityQueue<Integer> pq = new PriorityQueue<Integer>((p1, p2) -> p2 - p1);
- split string
1
2String[] words = paragraph.toLowerCase().split("\\W+");
//\\W means matches the nonword characters.
- Object[] toArray() &
T[] toArray(T[] a) 1
2
3
4
5Object[] a = collection.toArray();
String[] a = collection.toArray(new String[0]);
return collection.toArray(new String[0]);
String[] a = collection.toArray(new Sring[collection.size()]);
map.getOrDefault(key, value)
1
map.put(word, map.getOrDefault(word, 0) + 1);
Binary Search
Mid using (start + end) / 2 may cost integer overflow
should use start + (end - start) / 2
Traversal Map/Set in order
HashMap and Hashtable are in the “hash order that is conducive to random search”. They are saved not in input order. We can only output all while traversing, without order. We can even rehash() the hash again to get an internal order that is more conducive to random access.
Just use LinkedHashMap or LinkedHashSet
TreeSet
1
2
3
4
5
6
7
8
9
10
11// Return the least element in the set greater than or equal to e, or null.
set.ceiling(E e)
// Return the greatest element in the set less than or equal to e, or null.
set.floor(E e)
// Return the first(lowest) element in set.
set.first()
//Return the last(highest) element in set.
set.last()others
data type is not confimed if you do
1
var a = document.getElementById("id").value;
if you want a string, try this
1
a = a + "";
if you want a number type, try this
1
a = a * 1;
to be continued