Skip to content

Commit ffd9e29

Browse files
committed
perf: better scripts and easy mode to create question in project
1 parent 39675a8 commit ffd9e29

File tree

7 files changed

+140
-12
lines changed

7 files changed

+140
-12
lines changed

bin/lc.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,19 @@ program
2727
.option('-i, --identity <identity>', 'Specify a question by identity.')
2828
.option('-r, --random', 'Get a question randomly.')
2929
.option('-t, --today', 'Get a question today.')
30+
.option('-d, --directory <directory>', 'Set the question directory.')
3031
.parse(process.argv)
3132

3233
const cmdArgs = program.args;
3334
const cmdOpts = program.opts();
35+
// 根据dir 参数来设置基本目录
36+
const baseDir = cmdOpts.directory?path.join(process.cwd(),cmdOpts.directory):process.cwd();
3437
// 创建
3538
const create = (mode,question)=>{
36-
console.log(`MODE: ${mode}`)
39+
console.log(`MODE: ${mode}`);
3740
return new Promise(resolve=>{
3841
setQuestion(mode,question);
39-
const questionDir = path.join(process.cwd(),getQuestionFileName(question))
42+
const questionDir = path.join(baseDir,getQuestionFileName(question))
4043
createQuestion(question,questionDir).then(async (path)=>{
4144
if(!path)path = await createQuestionCopy(question,questionDir);
4245
console.log(`题目[${getQuestionChineseName(question)}]获取成功!\n题目文件地址为:${path}`)

bin/lk.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,17 @@ program
2323
.option('-i, --identity <identity>', 'Check the specified question by identity.')
2424
.option('-r, --random', 'Check the last random question.')
2525
.option('-t, --today', 'Check the question today.')
26+
.option('-d, --directory <directory>', 'Set the question directory.')
2627
.parse(process.argv)
2728

2829
const cmdArgs = program.args;
2930
const cmdOpts = program.opts();
31+
32+
// 根据dir 参数来设置基本目录
33+
const baseDir = cmdOpts.directory?path.join(process.cwd(),cmdOpts.directory):process.cwd();
3034
// 检测函数
31-
const check = async (mode,filePath,question)=>{
35+
const check = async (mode,question)=>{
36+
const filePath = path.join(baseDir,getQuestionFileName(question),'index.js');
3237
if(!fs.existsSync(filePath)) {
3338
console.log(`文件[${filePath}]不存在,请确保已经创建!`)
3439
}else{
@@ -41,22 +46,19 @@ const check = async (mode,filePath,question)=>{
4146
const callModeAction = {
4247
'today': async () => {
4348
const question = await getQuestionByMode("today");
44-
const filePath = path.join(process.cwd(),getQuestionFileName(question),'index.js');
45-
await check('today',filePath,question)
49+
await check('today',question)
4650
process.exit(0);
4751
},
4852
'random': async () => {
4953
const question = await getQuestionByMode("random");
50-
const filePath = path.join(process.cwd(),getQuestionFileName(question),'index.js');
51-
await check('today',filePath,question)
54+
await check('today',question)
5255
process.exit(0);
5356
},
5457
'identity': async (id) => {
5558
const question = !id?
5659
await getQuestionByMode(mode):
5760
await getQuestionById(id);
58-
const filePath = path.join(process.cwd(),getQuestionFileName(question),'index.js');
59-
await check('today',filePath,question)
61+
await check('today',question)
6062
process.exit(0);
6163
},
6264
}

common/utils/question-handler/fulfillQuestion.js

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {removeDomTags} from '../functions/removeDomTags.js';
33
import {getTestCase} from './getTestCase.js';
44
import {getQuestionUrl} from './getQuestionUrl.js';
55
import {createMarkdown} from './createMarkdown.js';
6+
import {getQuestionChineseName} from "#common/utils/question-handler/getQuestionChineseName.js";
67
/**
78
* @typedef {Object} Question
89
* @property {string} title

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@
1515
"lc": "bin/lc.js"
1616
},
1717
"scripts": {
18-
"leet-create": "node scripts/create.js",
19-
"leet-create:i": "node scripts/create.js -i",
20-
"leet-check": "node scripts/check.js",
18+
"leet-create": "node bin/lc.js -d src",
19+
"leet-check": "node bin/lk.js -d src",
20+
"leet-find": "node bin/lf.js -d src",
21+
"easy-find": "node common/view/finder.view.js",
2122
"easy-create": "node common/view/create.view.js",
2223
"easy-check": "node common/view/check.view.js",
2324
"commit": "cz",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<p>给你一棵二叉树的根&nbsp;<code>root</code>&nbsp;,请你将每个节点的值替换成该节点的所有 <strong>堂兄弟节点值的和&nbsp;</strong>。</p>
2+
3+
<p>如果两个节点在树中有相同的深度且它们的父节点不同,那么它们互为 <strong>堂兄弟</strong>&nbsp;。</p>
4+
5+
<p>请你返回修改值之后,树的根<em>&nbsp;</em><code>root</code><em>&nbsp;</em>。</p>
6+
7+
<p><strong>注意</strong>,一个节点的深度指的是从树根节点到这个节点经过的边数。</p>
8+
9+
<p>&nbsp;</p>
10+
11+
<p><strong>示例 1:</strong></p>
12+
13+
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/example11.png" style="width: 571px; height: 151px;" /></p>
14+
15+
<pre>
16+
<b>输入:</b>root = [5,4,9,1,10,null,7]
17+
<b>输出:</b>[0,0,0,7,7,null,11]
18+
<b>解释:</b>上图展示了初始的二叉树和修改每个节点的值之后的二叉树。
19+
- 值为 5 的节点没有堂兄弟,所以值修改为 0 。
20+
- 值为 4 的节点没有堂兄弟,所以值修改为 0 。
21+
- 值为 9 的节点没有堂兄弟,所以值修改为 0 。
22+
- 值为 1 的节点有一个堂兄弟,值为 7 ,所以值修改为 7 。
23+
- 值为 10 的节点有一个堂兄弟,值为 7 ,所以值修改为 7 。
24+
- 值为 7 的节点有两个堂兄弟,值分别为 1 和 10 ,所以值修改为 11 。
25+
</pre>
26+
27+
<p><strong>示例 2:</strong></p>
28+
29+
<p><img alt="" src="https://assets.leetcode.com/uploads/2023/01/11/diagram33.png" style="width: 481px; height: 91px;" /></p>
30+
31+
<pre>
32+
<b>输入:</b>root = [3,1,2]
33+
<b>输出:</b>[0,0,0]
34+
<b>解释:</b>上图展示了初始的二叉树和修改每个节点的值之后的二叉树。
35+
- 值为 3 的节点没有堂兄弟,所以值修改为 0 。
36+
- 值为 1 的节点没有堂兄弟,所以值修改为 0 。
37+
- 值为 2 的节点没有堂兄弟,所以值修改为 0 。
38+
</pre>
39+
40+
<p>&nbsp;</p>
41+
42+
<p><strong>提示:</strong></p>
43+
44+
<ul>
45+
<li>树中节点数目的范围是&nbsp;<code>[1, 10<sup>5</sup>]</code> 。</li>
46+
<li><code>1 &lt;= Node.val &lt;= 10<sup>4</sup></code></li>
47+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
/**
3+
* 2641.二叉树的堂兄弟节点 II [2024-02-07]
4+
* 给你一棵二叉树的根 root ,请你将每个节点的值替换成该节点的所有 堂兄弟节点值的和 。
5+
* 如果两个节点在树中有相同的深度且它们的父节点不同,那么它们互为 堂兄弟 。
6+
* 请你返回修改值之后,树的根 root 。
7+
* 注意,一个节点的深度指的是从树根节点到这个节点经过的边数。
8+
*
9+
* 示例 1:
10+
* 输入:root = [5,4,9,1,10,null,7]
11+
* 输出:[0,0,0,7,7,null,11]
12+
* 解释:上图展示了初始的二叉树和修改每个节点的值之后的二叉树。
13+
* - 值为 5 的节点没有堂兄弟,所以值修改为 0 。
14+
* - 值为 4 的节点没有堂兄弟,所以值修改为 0 。
15+
* - 值为 9 的节点没有堂兄弟,所以值修改为 0 。
16+
* - 值为 1 的节点有一个堂兄弟,值为 7 ,所以值修改为 7 。
17+
* - 值为 10 的节点有一个堂兄弟,值为 7 ,所以值修改为 7 。
18+
* - 值为 7 的节点有两个堂兄弟,值分别为 1 和 10 ,所以值修改为 11 。
19+
* 示例 2:
20+
* 输入:root = [3,1,2]
21+
* 输出:[0,0,0]
22+
* 解释:上图展示了初始的二叉树和修改每个节点的值之后的二叉树。
23+
* - 值为 3 的节点没有堂兄弟,所以值修改为 0 。
24+
* - 值为 1 的节点没有堂兄弟,所以值修改为 0 。
25+
* - 值为 2 的节点没有堂兄弟,所以值修改为 0 。
26+
*
27+
* 提示:
28+
* 树中节点数目的范围是 [1, 105] 。
29+
* 1 <= Node.val <= 104
30+
*
31+
*/
32+
/**
33+
* Definition for a binary tree node.
34+
* function TreeNode(val, left, right) {
35+
* this.val = (val===undefined ? 0 : val)
36+
* this.left = (left===undefined ? null : left)
37+
* this.right = (right===undefined ? null : right)
38+
* }
39+
*/
40+
/**
41+
* @param {TreeNode} root
42+
* @return {TreeNode}
43+
*/
44+
var replaceValueInTree = function(root) {
45+
46+
};
47+
48+
/**
49+
* Test case
50+
*/
51+
showLogs(
52+
replaceValueInTree,
53+
{
54+
data: [[ [5,4,9,1,10,null,7]],[ [3,1,2]]],
55+
structure: ["TreeNode"],
56+
},
57+
{
58+
data: [[0,0,0,7,7,null,11],[0,0,0]],
59+
structure: ["TreeNode"]
60+
}
61+
)
62+
console.log('点击跳转到题目提交:https://leetcode.cn/problems/cousins-in-binary-tree-ii/');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
/**
3+
* @题目
4+
* @描述
5+
*/
6+
// @Function
7+
8+
/**
9+
* Test case
10+
*/
11+
// @TestCase
12+
console.log('点击跳转到题目提交:@url');

0 commit comments

Comments
 (0)