动态

详情 返回 返回

LeetcodeJavaDebugEnhancer -- 一個用於Java的Leetcode算法題的本地調試增強器 - 动态 详情

LeetcodeJavaDebugEnhancer

🚀 一個用於Java的Leetcode算法題的本地調試增強器 🚀

Github地址

🎯 目標

  • 提供方便快速的調試功能。
  • 支持多樣的輸入源和輸出源。
  • 自動適配各種輸入參數類型。
  • 提供易維護、易拓展的API接口用於適配更多Leetcode算法調試場景。

🔧 下載與安裝

下載

Maven
<dependency>
    <groupId>io.github.jidcoo</groupId>
    <artifactId>leetcode-java-debug-enhancer</artifactId>
    <version>1.0.0</version>
</dependency>
Gradle
implementation 'io.github.jidcoo:leetcode-java-debug-enhancer:1.0.0'
Jar
資源 索引
託管倉庫 點擊這裏瀏覽本項目的託管倉庫
標準-Jar 點擊這裏直接下載(標準-Jar)
全量-Jar 點擊這裏直接下載(全量-Jar)

安裝

只需把LeetcodeJavaDebugEnhancer作為項目的庫引入即可。

運行要求

  • 支持的最低Java版本為Java 8
  • LeetcodeJavaDebugEnhancer依賴於Gson,版本為2.10.1。非Maven、非Gradle項目且項目本身不包含Gson庫的,請使用全量-Jar或者手動添加Gson庫到項目中。

🛠 基本使用

步驟1:

創建一個名為SimpleTest的Java類,並確保SimpleTest是public的:

//SimpleTest.java

public class SimpleTest {

}

步驟2:

在SimpleTest中導入io.github.jidcoo.opto.lcdb.enhancer.LeetcodeJavaDebugEnhancer,並聲明SimpleTest繼承自類LeetcodeJavaDebugEnhancer

//SimpleTest.java

import io.github.jidcoo.opto.lcdb.enhancer.LeetcodeJavaDebugEnhancer;

public class SimpleTest extends LeetcodeJavaDebugEnhancer {

}

步驟3:

把Leetcode網站上的題目的Java語言代碼粘貼到SimpleTest類中並編寫相應的算法代碼完成題目要求,這裏以題目兩數之和為例:

//SimpleTest.java

import io.github.jidcoo.opto.lcdb.enhancer.LeetcodeJavaDebugEnhancer;

public class SimpleTest extends LeetcodeJavaDebugEnhancer {
    
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            int n = nums.length;
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    if (nums[i] + nums[j] == target) {
                        return new int[]{i, j};
                    }
                }
            }
            return new int[0];
        }
    }

}

步驟4:

點擊SimpleTest的運行或調試按鈕從而運行SimpleTest,啓動調試增強器。

增強器啓動後你將會看到如下輸出:

LeetcodeJavaDebugEnhancer[1.0.0] started.
Case輸入規則:一個Case佔據一行,下一個Case需要在下一行輸入,一個Case輸入完成的標誌是遇到換行符或者EOF。

然後在控制枱中輸入調試參數Case:

[2,7,11,15] 9
[3,2,4] 6
[3,3] 6

然後增強器會根據輸入Case,運行Leetcode中的算法代碼,並輸出算法結果到控制枱:

[0,1]
[1,2]
[0,1]

📚 LeetcodeJavaDebugEnhancer功能詳述

1、支持多樣的輸入源

API
public InputProvider getInputProvider();
描述

LeetcodeJavaDebugEnhancer提供了對控制枱(ConsoleInputProvider)、文件/流(FileInputProvider)等多樣輸入源的支持。

LeetcodeJavaDebugEnhancer使用控制枱作為默認的輸入源。

如果你想要自定義輸入源,請通過重寫該方法返回一個有效的InputProvider

示例

假設現在有一個名為input.txt的文件,文件內容如下:

[2,7,11,15] 9
[3,2,4] 6
[3,3] 6

下面是一個把input.txt文件作為輸入源的示例代碼:

//SimpleTest.java

import io.github.jidcoo.opto.lcdb.enhancer.LeetcodeJavaDebugEnhancer;
import io.github.jidcoo.opto.lcdb.enhancer.base.InputProvider;
import io.github.jidcoo.opto.lcdb.enhancer.core.io.builtin.FileInputProvider;

import java.io.FileNotFoundException;

public class SimpleTest extends LeetcodeJavaDebugEnhancer {

    class Solution {
        public int[] twoSum(int[] nums, int target) {
            int n = nums.length;
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    if (nums[i] + nums[j] == target) {
                        return new int[]{i, j};
                    }
                }
            }
            return new int[0];
        }
    }

    @Override
    public InputProvider getInputProvider() {
        try {
            // FileInputProvider can accept file-name, file-object, and input-stream as construction parameters.
            return new FileInputProvider("input.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}

2、支持多樣的輸出源

API
public OutputConsumer getOutputConsumer();
描述

LeetcodeJavaDebugEnhancer提供了對控制枱(ConsoleOutputConsumer)、文件/流(FileOutputConsumer)等多樣輸入源的支持。

LeetcodeJavaDebugEnhancer使用控制枱作為默認的輸出源。

如果你想要自定義輸出源,請通過重寫該方法返回一個有效的OutputConsumer

示例

接着上面的示例,下面是一個把input.txt文件作為輸入源,output.txt文件作為輸出源的示例代碼:

//SimpleTest.java

import io.github.jidcoo.opto.lcdb.enhancer.LeetcodeJavaDebugEnhancer;
import io.github.jidcoo.opto.lcdb.enhancer.base.InputProvider;
import io.github.jidcoo.opto.lcdb.enhancer.base.OutputConsumer;
import io.github.jidcoo.opto.lcdb.enhancer.core.io.builtin.FileInputProvider;
import io.github.jidcoo.opto.lcdb.enhancer.core.io.builtin.FileOutputConsumer;

import java.io.FileNotFoundException;

public class SimpleTest extends LeetcodeJavaDebugEnhancer {

    class Solution {
        public int[] twoSum(int[] nums, int target) {
            int n = nums.length;
            for (int i = 0; i < n; ++i) {
                for (int j = i + 1; j < n; ++j) {
                    if (nums[i] + nums[j] == target) {
                        return new int[]{i, j};
                    }
                }
            }
            return new int[0];
        }
    }

    @Override
    public InputProvider getInputProvider() {
        try {
            // FileInputProvider can accept file-name, file-object, and input-stream as construction parameters.
            return new FileInputProvider("input.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public OutputConsumer getOutputConsumer() {
        try {
            // FileOutputConsumer can accept file-name, file-object, and ouput-stream as construction parameters.
            return new FileOutputConsumer("output.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}

3、支持自定義調試增強入口點

API
public Method getEnhancementPoint();
描述

眾所周知,在Leetcode上有一些算法題目為數據結構設計題,例如最小棧、用隊列實現棧等。

上述這類題目對目前的調試增強器並不友好,因為這類題目並沒有一個特別明確的算法入口,調試增強器很難找到一個有效的增強切入點。

因此針對這類題目的調試,請 盡最大可能地 通過重寫該方法為調試增強器提供一個來自當前public類的(例如上面示例中的SimpleTest)、有效的、明確的增強切入點Method,調試增強器將會從該增強點切入執行調試增強邏輯,從而實現代碼調試的目的。

示例

假設現在有一個名為input.txt的文件,文件內容如下:

["MyStack","push","push","top","pop","empty"] [[],[1],[2],[],[],[]]

下面是一個把input.txt文件作為輸入源,並自定義實現增強點 runHere(String[] operations, int[][] numbers) 方法進行調試的示例代碼:

//SimpleTest.java

import io.github.jidcoo.opto.lcdb.enhancer.LeetcodeJavaDebugEnhancer;
import io.github.jidcoo.opto.lcdb.enhancer.base.InputProvider;
import io.github.jidcoo.opto.lcdb.enhancer.core.io.builtin.FileInputProvider;
import io.github.jidcoo.opto.lcdb.enhancer.utils.ReflectUtil;

import java.io.FileNotFoundException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class SimpleTest extends LeetcodeJavaDebugEnhancer {

    class MyStack {
        Queue<Integer> queue1;
        Queue<Integer> queue2;

        /**
         * Initialize your data structure here.
         */
        public MyStack() {
            queue1 = new LinkedList<Integer>();
            queue2 = new LinkedList<Integer>();
        }

        /**
         * Push element x onto stack.
         */
        public void push(int x) {
            queue2.offer(x);
            while (!queue1.isEmpty()) {
                queue2.offer(queue1.poll());
            }
            Queue<Integer> temp = queue1;
            queue1 = queue2;
            queue2 = temp;
        }

        /**
         * Removes the element on top of the stack and returns that element.
         */
        public int pop() {
            return queue1.poll();
        }

        /**
         * Get the top element.
         */
        public int top() {
            return queue1.peek();
        }

        /**
         * Returns whether the stack is empty.
         */
        public boolean empty() {
            return queue1.isEmpty();
        }
    }

    /**
     * Enhancement point function.
     *
     * @param operations
     * @param numbers
     * @return
     */
    public List<Object> runHere(String[] operations, int[][] numbers) {
        List<Object> ans = new ArrayList<>();
        MyStack stack = null;
        for (int i = 0; i < operations.length; i++) {
            String operation = operations[i];
            Object curReturn = null;
            switch (operation) {
                case "MyStack":
                    stack = new MyStack();
                    break;
                case "push":
                    stack.push(numbers[i][0]);
                    break;
                case "top":
                    curReturn = stack.top();
                    break;
                case "pop":
                    curReturn = stack.pop();
                    break;
                case "empty":
                    curReturn = stack.empty();
                    break;
            }
            ans.add(curReturn);
        }
        return ans;
    }

    @Override
    public Method getEnhancementPoint() {
        // Return the runHere() method object.
        // By the way, you can use ReflectUtil.getMethod() to easily obtain the specified enhancement point method of a class.
        return ReflectUtil.getMethod(SimpleTest.class, "runHere", String[].class, int[][].class);
    }

    @Override
    public InputProvider getInputProvider() {
        try {
            return new FileInputProvider("input.txt");
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

}

對於這種數據結構設計題目,public Method getEnhancementPoint()的設計思路是提供一種迂迴但有用的方式讓用户能夠完成對此類題目的調試。

但不難發現的是,這樣的設計可能會給用户增加額外的、意義不大的編碼任務。

因此,在後續的版本迭代中,我們將嘗試對這類數據結構設計題目場景進行抽象,把上述的“可能會給用户增加額外的、意義不大的編碼任務”的過程交給調試增強器來自動完成!!!

請持續關注該項目,敬請期待 !!! 🎉 🎉 🎉

🐛 問題與反饋

關於問題

LeetcodeJavaDebugEnhancer使用GitHub的集成問題跟蹤系統來記錄缺陷和新特性功能請求。如果你想提出一個Issue,請遵循如下的推薦建議:

  • 在你提出Issue前,請先通過搜索Issue Tracker,看看是否已經有人報告了相關的Issue。
  • 如果沒有相關的Issue記錄,請創建一個新的issue。
  • 在Issue報告中提供儘可能多的信息,讓我能夠快速瞭解你當前使用的如調試增強器版本、Java版本、Leetcode題目、輸入、輸出、異常堆棧輸出等信息。
  • 如果你需要粘貼代碼或者包含堆棧信息的文本,請在相應文本前和文本後使用Markdown的代碼塊\```進行轉義。
  • 如果可以的話,請給出一個能夠復現問題的測試Case,並將其附加到Issue上。

關於反饋

有關LeetcodeJavaDebugEnhancer的任何反饋、建議或者新特性功能請求,可以通過提出Issue的方式向我反饋。

當然,你也可以直接通過郵件的方式聯繫我:

暱稱: Jidcoo

郵箱: jidcoo@163.com

🎉 貢獻

歡迎任何人對此項目的任何形式的貢獻!!!無論是新特性功能請求、Issue報告、代碼提交、文檔或其他類型的貢獻或支持!!!

開源的快樂不就來自於此嗎?!!!

所有提交的內容都需要經過代碼審查,所以我們使用Github的pull requests。有關PullRequest的使用,你可以查閲Github的幫助文檔。

📜 許可證

LeetcodeJavaDebugEnhancer是基於Apache 2.0 license發佈的開源項目。

Copyright 2024-2026 Jidcoo(https://github.com/jidcoo).

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Add a new 评论

Some HTML is okay.