LeetCodeDailyNotes
LeetCode Daily Notes#
6/3/2020#
string.trim() is used to trim leading and tailing spaces in a String
If we want to split a string by multiple spaces we can use string.split(“\\s+”)
If we want to concatenate a list of string, we can use String.join(“ “, wordList).
Lambda expression used in sorting List or Array
1
2
3
4
5
6
7
8
9
10
11
12
13arr1.sort((a, b) -> Integer.compare(a[1] - a[0], b[1] - b[0]));
// or
arr1.sort(Comparator.comparingInt(a -> a[1] - a[0]));
// or this method is more flexible
arr2.sort((a, b) -> {
if(a[0] > a[1]) {
return 1;
} else if (a[0] < a[1]) {
return -1;
} else {
return 0;
}
});