神器级扩展。
注意这个扩展依赖nodejs环境,其它没什么。
本地java环境+安装两个vs code扩展:
/*
* @lc app=leetcode.cn id=1 lang=java
*
* [1] 两数之和
*/
// @lc code=start
class Solution {
public int[] twoSum(int[] nums, int target) {
int[] res = new int[2];
for (int i = 0; i < nums.length; i++) {
int dif = target - nums[i];
// j = i + 1 的目的是减少重复计算和避免两个元素下标相同
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] == dif){
res[0] = i;
res[1] = j;
return res;
}
}
}
return res;
}
}
// @lc code=end
class Main {
public static void main(String[] args) {
// Create a new Solution instance
Solution solution = new Solution();
// Create a test case
int[] nums = {2,7,11,15};
int target = 9;
// Get the answer
int[] answer = solution.twoSum(nums, target);
// Print the answer
System.out.println("answer");
for (int i = 0; i < answer.length; i++) {
System.out.println(answer[i]);
}
}
}
这里有一点需要注意的是,由于 LeetCode 生成的答题模板的类名均为 Solution,因此会造成同一个目录下存在多个同名类的情况出现,可能导致代码无法正确执行,因此如果希望调试 LeetCode Java 代码,但当前目录又存在有多个 LeetCode Java 文件时,需要保证类名的唯一性,我们可以把被调试的 Solution 类改一个名字(但要记住提交时把名字改回来),或者干脆拷贝到另一个干净的目录下调试即可。
vs code终端中cd到leetcode目录,用以下命令创建dontnet console工程
dotnet new console -o "cspj"
也可以这样
mkdir cspj
cd cspj
dotnet new console
cd到刚刚创建的cspj目录,然后执行run(run之前会自动build),检查工程能否正常编译/执行。
可以看到默认生成的代码输出了Hello World!。
另外,执行成功之后,会看到cspj下生成了bin文件夹。
Ps:编译命令为dotnet build
launch.json,定义点debug时怎么干,干什么。
task.json,这里用到的是,由launch.json调用,调用里面的任务,用于build(即调试前先编译最新代码)。
如果执行过dotnet run,且vscode工作区路径为cspj目录(而非.leetcode目录)vs code会自动配置launch和task,如下图操作。
注意,最后如果使用.leetcode目录为工作区,要把cspj下的.vscode下的launch.json和task.json搬到.leetcode下的.vscode下,同时,改两个json中的路径,具体为加“/cspj”。
如果一开始已经有launch.json,这时需要半自动配置。
1、添加一个.NET Core Launch (console)启动配置
2、修改program字段
由
${workspaceFolder}/bin/Debug/< target-framework>/
改为
${workspaceFolder}/cspj/bin/Debug/netcoreapp2.2/cspj.dll
说明一下,
3、创建task.json
如果已经有在用的task,就备份一下,按下文创建完再把之前task中的内容整合到新的task中。
4、添加args
参数中添加目标路径 “${workspaceRoot}\cspj\cspj.csproj”
添加完是这样子:
修改Program.cs。
如下:
using System;
using System.Collections;
public class Solution
{
public int[] TwoSum(int[] nums, int target)
{
Hashtable map = new Hashtable();
for (int i = 0; i < nums.Length; i++)
{
int complement = target - nums[i];
if (map.ContainsKey(complement))
{
return new int[] { Convert.ToInt32(map[complement]), i };
}
if (!map.ContainsKey(nums[i]))
{
map.Add(nums[i], i);
}
}
return null;
}
}
namespace cspj
{
class Program
{
static void Main(string[] args)
{
Solution solution = new Solution();
// Create a test case
int[] nums = { 2, 7, 11, 15 };
int target = 9;
// Get the answer
int[] answer = solution.TwoSum(nums, target);
// Print the answer
Console.WriteLine("answer");
for (int i = 0; i < answer.Length; i++)
{
Console.WriteLine(answer[i]);
}
}
}
}
用vs code的debug来启动
最后, 我的目录结构长这样子:
刷题的流程是,
1、用leetcode插件生成题目文件1.两数之和.cs
2、拷贝1.两数之和.cs中的Solution到Progam.cs中编码并调试、测试。
3、把Progam中的写好Solution类拷贝到1.两数之和.cs中,使用leetcode插件提交到leetcode官网。
这里直接下载我配置好的完整的vs code工作区,可以参考launch/task的配置,以及调试入口,另外注意本地的扩展和环境要自己搞好。
Java版:
如何在 VS Code 中调试 LeetCode 代码
CSharp版:
C#刷遍Leetcode面试题系列连载(1) - 入门与工具简介
使用Visual Studio Code创建asp.net core项目
使用VsCode编写和调试.NET Core项目
手工编辑 tasks.json 和 launch.json,让你的 VSCode 具备调试 .NET Core 程序的能力