I would like to share with leetcode community a handy method I use to debug my code.
So, instead of typing System.out.println(x) to print out the variable x you can simply type log(x).
It will print the value of x along with line of code at which the log function is called.
This very handy and can cover the need to use the premium Leetcode debugger.
The method also is customized to print arrays in list format by using Arrays.toString() method.
private static <T> void log(T ...x){
if (true) { // change to false to disable all log output
var sb = new StringBuilder();
int line = Thread.currentThread().getStackTrace()[2].getLineNumber()-47;
sb.append("L");
sb.append(line);
sb.append(":");
for(T v : x) {
sb.append(" ");
if(v == null)
sb.append("<null>");
else if(v.getClass().isInstance(new int[0]))
sb.append(Arrays.toString((int[])v));
else if(v.getClass().isInstance(new boolean[0]))
sb.append(Arrays.toString((boolean[])v));
else if(v instanceof ListNode)
sb.append(((ListNode)v).val);
else
sb.append(v);
}
System.out.println(sb.toString());
}
}Looking forward to your comments and suggestions.
If you like, please upvote this post!