如何更愉快地在 VSCode 中刷 LeetCode

1,210 阅读1分钟

我的环境

SYSTEM:macOS 11.2.3

LANGUAEG:Rust

如果不能通过账号登陆 LeetCode,可以走 Cookie 登陆

change code template

需要修改 ~/.vscode/extensions/leetcode.vscode-leetcode-0.18.0/node_modules/vsc-leetcode-cli/templates/codeonly.tpl

#[allow(dead_code)]
pub struct Solution {}

${comment.start}
${comment.line} @lc app=${app} id=${fid} lang=${lang}
${comment.line}
${comment.line} [${fid}] ${name}
${comment.end}

${comment.singleLine} @lc code=start
${code}
${comment.singleLine} @lc code=end

how debug in local

调整 setting.json 中 leetcode 配置

"leetcode.workspaceFolder": "/Users/yiniau/project/leetcode",
"leetcode.endpoint": "leetcode",
"leetcode.filePath": {
"rust": {
    "folder": "./src",
    "filename": "${snake_case_name}_${id}.${ext}"
},
"default": {
	"folder": "",
	"filename": "${id}.${kebab-case-name}.${ext}"
}
},
"leetcode.defaultLanguage": "rust"

拉下来问题代码之后会在 src 下出现对应文件,以 605 问题为例

src
|- can_place_flowers_605.rs
|- main.rs
Cargo.toml
Cargo.lock

需要手动在 main 中加入引用 和 编写测试代码, 直接复制文件名就好了,在mac上选中文件然后回车、 Ctrl—V 一下就行

#[path="./can_place_flowers_605.rs"]
mod can_place_flowers_605;

fn main() {
    println!("hello world");
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn can_place_flowers_605() {
        assert_eq!(
            can_place_flowers_605::Solution::can_place_flowers(vec![1,0,0,0,1], 1),
            true
        );
        assert_eq!(
            can_place_flowers_605::Solution::can_place_flowers(vec![1,0,0,0,1], 2),
            false
        );
    }
}