Idea插件 之 LeetCode Editor
文档
LeetCode Editor插件中文官网:https://github.com/shuzijun/leetcode-editor/blob/master/README_ZH.md
获取
配置(第一次安装需要先配置)
官网描述:
自己配置:
官方给的下图中第5、6步一个模板:https://github.com/shuzijun/leetcode-editor/blob/master/doc/CustomCode_ZH.md
我设置的文件名:
P${question.frontendQuestionId}_${question.title}/Main
文件模板:
package leetcode.editor.cn.P${question.frontendQuestionId}_${question.title};
public class Main{
public static void main(String[] args) {
Solution solution = new Solution();
}
}
${question.code}
图示:
然后点这里选题:
工具类:
ListNode :
public class ListNode {
public int val;
public ListNode next;
public ListNode() {}
public ListNode(int val) { this.val = val; }
public ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
TreeNode
public class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode() {}
public TreeNode(int val) { this.val = val; }
public TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
自定义工具类:
力扣上刷题,想复制它的测试案例,直接在本地测试?不妨复制这个工具类
import com.alibaba.fastjson.JSONObject;
import com.ljy.commons.ListNode;
import com.ljy.commons.TreeNode;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.*;
public class LeetCodeUtil {
/**
* 根据数组生成链表
*
* @param arr
* @return
*/
public static ListNode getListNode(int[] arr) {
ListNode listNode = new ListNode(arr[0]);
ListNode head = listNode;
for (int i = 1; i < arr.length; i++) {
ListNode node = new ListNode(arr[i]);
listNode.next = node;
listNode = node;
}
listNode.next = null;
return head;
}
/**
* 打印链表
*
* @param node
*/
public static void printListNode(ListNode node) {
while (node != null) {
String result = node.next == null ? node.val + "" : node.val + " --> ";
node = node.next;
System.out.print(result);
}
}
/**
* 根据数组生成二叉树
*
* @param arr
* @return
*/
public static TreeNode getTreeNode(Integer[] arr) {
return getTreeNode(arr, 0);
}
/**
* 根据数组生成二叉树
*
* @param data
* @return
*/
public static TreeNode getTreeNode(String data) {
String[] strArr = string2stringArray(data)[0];
Integer[] integerArr = new Integer[strArr.length];
for (int i = 0; i < strArr.length; i++) {
integerArr[i] = "null".equals(strArr[i]) ? null : Integer.parseInt(strArr[i]);
}
return getTreeNode(integerArr);
}
//层次法生成二叉树
private static TreeNode getTreeNode(Integer[] arr, int index) {
TreeNode node = null;
if (index < arr.length) {
if (arr[index] != null) {
node = new TreeNode(arr[index]);
node.left = getTreeNode(arr, 2 * index + 1);
node.right = getTreeNode(arr, 2 * index + 2);
}
}
return node;
}
//层次遍历
public static void printTreeNode(TreeNode treeNode) {
System.out.println("注:/、\\分别表示左子树、右子树");
LinkedList<TreeNode> linkedList = new LinkedList<>();
linkedList.add(treeNode);
while (true) {
if (linkedList == null || linkedList.size() == 0) break;
//输出
for (TreeNode t2 : linkedList) {
System.out.print(String.format("%4s", t2.val));
}
System.out.println();
//用于画线
for (TreeNode t2 : linkedList) {
StringBuilder sb = new StringBuilder();
if (t2.left != null)
sb.append("/");
if (t2.right != null)
sb.append("\\");
System.out.print(String.format("%4s", sb.toString()));
}
System.out.println();
//下一层
linkedList = printTreeNode(linkedList);
}
}
//根据i层获得i+1层
private static LinkedList<TreeNode> printTreeNode(LinkedList<TreeNode> linkedList) {
LinkedList<TreeNode> l2 = new LinkedList<>();
if (linkedList == null || linkedList.size() == 0) {
return null;
}
for (TreeNode t : linkedList) {
if (t.left != null)
l2.add(t.left);
if (t.right != null)
l2.add(t.right);
}
return l2;
}
/**
* 对二叉树的节点排序并打印
*
* @param root
*/
public static void printSortTreeNode(TreeNode root) {
List list = new ArrayList();
preOrder(root, list);
Collections.sort(list);
System.out.println(list);
}
/**
* 二叉树先序遍历(非递归)
*
* @param root
*/
private static void preOrder(TreeNode root, List list) {
Stack<TreeNode> stack = new Stack<>();
while (true) {
while (root != null) {
list.add(root.val);
stack.push(root);
root = root.left;
}
if (stack.isEmpty()) break;
root = stack.pop();
root = root.right;
}
}
/**
* 打印数组
*
* @param arr
*/
public static void printArr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + (i == arr.length - 1 ? "" : ","));
}
}
public static void quickSort(int[] arr) {
quickSort(arr, 0, arr.length - 1);
}
/**
* 快排
*
* @param arr
* @param leftIndex
* @param rightIndex
*/
public static void quickSort(int[] arr, int leftIndex, int rightIndex) {
if (leftIndex >= rightIndex) {
return;
}
int left = leftIndex;
int right = rightIndex;
//待排序的第一个元素作为基准值
int key = arr[left];
//从左右两边交替扫描,直到left = right
while (left < right) {
while (right > left && arr[right] >= key) {
//从右往左扫描,找到第一个比基准值小的元素
right--;
}
//找到这种元素将arr[right]放入arr[left]中
arr[left] = arr[right];
while (left < right && arr[left] <= key) {
//从左往右扫描,找到第一个比基准值大的元素
left++;
}
//找到这种元素将arr[left]放入arr[right]中
arr[right] = arr[left];
}
//基准值归位
arr[left] = key;
//对基准值左边的元素进行递归排序
quickSort(arr, leftIndex, left - 1);
//对基准值右边的元素进行递归排序。
quickSort(arr, right + 1, rightIndex);
}
/**
* 字符串转二维数组
*
* @param s 例如:[[1,0],[99,97]]
* @return 二维数组
*/
public static int[][] string2intArray(String s) {
String[][] strings = string2stringArray(s);
int[][] res = new int[strings.length][strings[0].length];
String[] tmp;
for (int i = 0; i < strings.length; i++) {
tmp = strings[i];
for (int j = 0; j < tmp.length; j++) {
res[i][j] = Integer.parseInt(tmp[j]);
}
}
return res;
}
/**
* 字符串转二维数组
*
* @param s 例如:[[1,0],[99,97]]
* @return 二维数组的第0个
*/
public static int[] string2intArray0(String s){
return string2intArray(s)[0];
}
/**
* 字符串转二维数组。默认每一行数据个数相同
*
* @param s 例如:[["1","0"],["99","97"]]
* @return 二维数组
*/
public static String[][] string2stringArray(String s) {
return string2stringArray(s,true);
}
/**
* 字符串转二维数组。isSameLength = true 表示每一行数据个数相同
* @param s 例如:[["1","0"],["99","97"]]
* @param isSameLength 每一行的元素个数是否相同
* @return
*/
public static String[][] string2stringArray(String s, boolean isSameLength) {
// [[1,0],[99,97]]
//去掉首尾的[]
s = s.substring(s.indexOf("[") + 1, s.lastIndexOf("]"));
//去掉所有的双引号
s = s.replaceAll("\"", "");
//使用,分隔 ==> [1,0]
String[] row = s.split("],\\[");
//每一行右相同个数的元素
if (isSameLength){
String[][] res = new String[row.length][];
int colCount = row[0].split(",").length;
for (int i = 0; i < row.length; i++) {
res[i] = new String[colCount];
String[] col = row[i].replace("[", "").replace("]", "").split(",");
System.arraycopy(col, 0, res[i], 0, col.length);
}
return res;
}else {
String[][] res = new String[row.length][];
for (int i = 0; i < row.length; i++) {
res[i] = row[i].replace("[", "").replace("]", "").split(",");
}
return res;
}
}
/**
* 模拟力扣中的执行方法:
* 例如:
* ["MyLinkedList","addAtHead","addAtTail","addAtIndex","get","deleteAtIndex","get"]
* [[],[1],[3],[1,2],[1],[1],[1]]
*
* @param obj 执行哪个对象的方法?
* @param method 方法的集合,例如:["MyLinkedList","addAtHead","addAtTail","addAtIndex","get","deleteAtIndex","get"]
* @param param 参数的集合,例如:[[],[1],[3],[1,2],[1],[1],[1]]
* @return 依次执行所有方法后的obj
* @throws Exception
*/
public static Object invoke(Object obj, String method, String param) throws Exception {
//["Trie","insert","search","search","startsWith","insert","search"]
// [[],["apple"],["apple"],["app"],["app"],["app"],["app"]]
String[] ms = string2stringArray(method)[0];
String[][] ps = string2stringArray(param, false);
//ps中空字符串转null
for (int i = 0; i < ps.length; i++) {
ps[i] = ps[i].length == 1 && ps[i][0].length() == 0 ? null : ps[i];
}
Class<?> objClass = obj.getClass();
for (int i = 0; i < ms.length; i++) {
String mi = ms[i];
//如果以大写字母开头,则调用构造器
int len = ps[i] == null ? 0 : ps[i].length;
if (mi.charAt(0) <= 'Z') {
for (Constructor<?> constructor : objClass.getConstructors()) {
int parameterCount = constructor.getParameterCount();
if (parameterCount == len) {
System.out.println("==>构造器:" + constructor.getName() +
Arrays.toString(ps[i])
.replace("[", "(").replace("]", ")")
);
constructor.setAccessible(true);
obj = constructor.newInstance(ps[i]);
break;
}
}
} else {
//调用方法,传参数
Method[] methods = objClass.getMethods();
for (Method md : methods) {
//参数个数
int parameterCount = md.getParameterCount();
//参数名一致,参数个数一致,很可能就是这个方法
if (md.getName().equals(mi) && parameterCount == len) {
md.setAccessible(true);
//转化String数组类型的参数到我们想要的类型:
Class<?>[] pts = md.getParameterTypes();
Object[] psObj = new Object[pts.length];
for (int k = 0; k < pts.length; k++) {
psObj[k] = cast(pts[k], ps[i][k]);
}
System.out.println("==> 方法:" + md.getName() + "" +
Arrays.toString(ps[i])
.replace("[", "(").replace("]", ")"));
//真正的开始执行方法:
Object res = md.invoke(obj, psObj);
System.out.println("<== " + res);
break;
}
}
}
}
return obj;
}
/**
* 将字符串s转换为指定的class类型
* @param clzz
* @param s
* 依赖于fastjson,可在pom中引入:
* <dependency>
* <groupId>com.alibaba</groupId>
* <artifactId>fastjson</artifactId>
* <version>1.2.75</version>
* </dependency>
* @return
*/
private static Object cast(Class<?> clzz, String s) {
String jsonString = JSONObject.toJSONString(s);
return JSONObject.parseObject(jsonString, clzz);
}
}