From JavaDoc for Stack, we could see: "A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:" Deque<Integer> stack = new ArrayDeque<>(); Here are some answers from Why should I use Deque over Stack?. Inconsistency: …
Recover Binary Search Tree
This is No.99 in LeetCode. Here are approaches to this problem: Approach 1: Sort an Almost Sorted Array Where Two Elements Are Swapped Algorithm Here is the algorithm: Construct inorder traversal of the tree. It should be an almost sorted list where only two elements are swapped. Identify two swapped elements x and y in …
How to get random number in a range in Java
Let’s use the Math.random method to generate a random number in a given range: public int getRandomNumber(int min, int max) { return (int) ((Math.random() * (max – min)) + min); } Why does that work? Well, let’s look at what happens when Math.random returns 0.0, it’s the lowest possible output: 0.0 * (max – min) …
Continue reading “How to get random number in a range in Java”
How to sort 2D array in Java
My code : Arrays.sort(intervals, new Comparator<int[]>(){ @Override public int compare( int[] a, int[] b ){ return a[0]==b[0] ? a[1] – b[1] : a[0] – b[0]; // return Integer.compare( a[0], b[0] ); } }); And here is an simple way: Arrays.sort(myArr, (a, b) -> a[0] – b[0]);
Sort List
This is No.148 Problem in LeetCode. Here are two approaches as below: Top Down Merge Sort Recursively split the original list into two halves. The split continues until there is only one node in the linked list (Divide phase). To split the list into two halves, we find the middle of the linked list using …
How to construct a BST with a given pre-order array?
Code is below: // build the tree private TreeNode build_tree( String[] node, int min, int max ){ // invalid corner case if( idx >= node.length ){ return null; } TreeNode root = null; // valid value if( Integer.valueOf(node[idx]) > min && Integer.valueOf(node[idx]) < max ){ // build the root root = new TreeNode( Integer.valueOf( node[idx]) …
Continue reading “How to construct a BST with a given pre-order array?”
How to find Maximum AND XOR value of two elements in array?
I always feel confused when I face bit manipulation algorithm problems. Even the solution post in discussion forum is hard for me to understand. When I was trying to figure out the daily challenge, ofc it is a bit manipulation problem, I do more research because I still can’t understand the solution post in discussion …
Continue reading “How to find Maximum AND XOR value of two elements in array?”
An example to overwrite comparator in method to customize sorting of array
I still feel confused when I want to customize the way of sorting for array and collections in java. And here is a good example of how to write a customized sorting function to sort string array: Comparator<String> comp = new Comparator<String>(){ @Override public int compare(String str1, String str2){ String s1 = str1 + str2; …
Continue reading “An example to overwrite comparator in method to customize sorting of array”
what is difference between “wrap content” and “match constraint” in android studio
fill_parent (deprecated and renamed MATCH_PARENT in API Level 8 and higher) Setting the layout of a widget to fill_parent will force it to expand to take up as much space as is available within the layout element it’s been placed in. It’s roughly equivalent of setting the dockstyle of a Windows Form Control to Fill. …
Conversion between array & Set, StringBuilder & String
Array & Set Example: Character vowel[] = { 'a', 'A', 'e', 'E', 'i', 'I', 'o', 'O', 'u', 'U' }; Set<Character> v = new HashSet<>(Arrays.asList(vowel)); StringBuilder & String Example: StringBuffer sb = new StringBuffer(); String str = sb.toString();