Skip to content

Commit b225477

Browse files
committed
fix:修复showLogs参数无法接收多个内容的问题并只保留leet-check和leet-create
1 parent 88de90a commit b225477

File tree

11 files changed

+128
-70
lines changed

11 files changed

+128
-70
lines changed

Readme.md

+52-31
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
- [x] 4.函数:优化时间和资源统计函数
1212
- [ ] 5.优化创建时的体验,添加重复时候的确认覆盖或者添加额外符号
1313
- [ ] 6.特殊数据结构的处理(ListNode,Stack等)的处理
14-
- [ ] 7.创建某一特定编号的题目脚本
15-
- [ ] 8.题目中图片的预览功能
14+
- [ ] 7.创建某一特定编号的题目脚本,以及实现随机题目
15+
- [ ] 8.实现在编辑器中预览图片
1616

1717
## How to use for yourself ?
1818

@@ -38,33 +38,46 @@ pnpm install
3838
### 1. 获取今天的题目
3939

4040
```shell
41-
yarn create-today [your-specified-name:default is question's id]
41+
yarn leet-create
4242
```
43-
4443
会通过接口获取今日题目,并会在`src`目录下根据`你提供的指定名称(默认不填值为题目的id)`创建一个目录,并将今日题目和基础示例代码填充到`index.js`中。
4544

46-
```shell
47-
//todo 填充代码创建结果和截图
48-
```
45+
![创建目录](./resources/leet-create-0.png)
46+
![填充文件](resources/leet-create-1.png)
4947

5048
### 2. 检验今天的题目
5149

5250
```shell
53-
yarn check-today
51+
yarn leet-check
5452
```
5553

5654
此指令会根据今天你的题目创建时填写的名称去执行对应的题目文件,输出结果。
5755
> NOTE:缓存的实现是在`commom/resouces/store.json`,如果只想让内容在本地存在,不上传到个人项目中的话,执行`git update-index --aussume-unchanged common/resources/store.json`来忽略本地的文件变更即可。
56+
>
5857
5958
```shell
60-
// todo 填充代码创建结果和截图
61-
```
59+
yarn leet-check
60+
61+
[leet-check]检测题目:2744.find-maximum-number-of-string-pairs
62+
执行结果:
63+
┌─────────┬──────────┬──────────┬──────────┬────────────┬───────────┐
64+
│ (index) │ 测试结果 │ 预期结果 │ 执行结果 │ 执行用时 │ 内存占用 │
65+
├─────────┼──────────┼──────────┼──────────┼────────────┼───────────┤
66+
│ 0 │ '通过''2''2''0.2113ms''1.68 KB'
67+
│ 1 │ '通过''1''1''0.0195ms''1.73 KB'
68+
│ 2 │ '通过''0''0''0.0091ms''1.80 KB'
69+
└─────────┴──────────┴──────────┴──────────┴────────────┴───────────┘
70+
点击跳转到题目提交:https://leetcode.cn/problems/find-maximum-number-of-string-pairs/
6271

72+
Done in 0.18s.
73+
74+
```
75+
![检测题目](resources/leet-check.png)
6376
### 3. 创建自己想要练习的题目
6477

6578
```shell
6679
# 可以使用你喜欢的任意包管理工具,例如 `pnpm`/`npm`
67-
yarn leet-create [题目编号或者名称]
80+
yarn leet-create -i [题目编号或者名称]
6881

6982
e.g.
7083
yarn leet-create 1314
@@ -78,11 +91,13 @@ yarn leet-create 1314
7891
└── index.js # 模板js文件 可以替换题目
7992
```
8093

94+
//todo 待填充
95+
8196
### 4. 检验自己想要练习的结果
8297

8398
```shell
8499
# 可以使用你喜欢的任意包管理工具,例如 `pnpm`/`npm`
85-
yarn leet-check [题目编号或者名称]
100+
yarn leet-check -i [题目编号或者名称]
86101

87102
e.g.
88103
yarn leet-check 1314
@@ -91,31 +106,37 @@ yarn leet-check 1314
91106
会出现其用时以及内存占用:
92107

93108
```shell
94-
yarn run v1.22.19
95-
$ node common/scripts/check.js 2696
96-
执行结果:
97-
5
98-
函数执行用时: 2.509ms
99-
内存占用:262144
100-
2
101-
函数执行用时: 0.063ms
102-
内存占用:0
103-
104-
✨ Done in 0.13s.
109+
//todo 待填充
110+
```
111+
### 5. 获取随机题目
112+
113+
```shell
114+
yarn leet-create -r
115+
```
116+
会获取一个当前src目录中未出现过的题目
117+
//todo 待填充
118+
119+
### 6. 检验随机题目
120+
121+
```shell
122+
# 可以使用你喜欢的任意包管理工具,例如 `pnpm`/`npm`
123+
yarn leet-check -r
124+
```
125+
会检验上次随机出的题目编号:
126+
127+
```shell
128+
//todo 待填充
105129
```
106130

107131
## Scripts API Document
108132

109-
| script | 参数 | 说明 |
110-
|--------------|------------|----------|
111-
| leet-create | 题目编号或者题目名称 | 创建一个模板文件 |
112-
| leet-check | 题目编号或者题目名称 | 执行某个项目 |
113-
| get-question | 无 | 获取今日题目对象 |
114-
| create-today | 无 | 创建今日题目 |
115-
| check-today | 无 | 检查今日题目 |
133+
| script | 参数 | 说明 |
134+
|-------------|------------------|-----------------------------------------------------|
135+
| leet-create | [-i [题目编号]][-r] | 创建一个题目,不带参数获取今日题目,`-i` 参数指定题号去获取,`-r` 随机题目 |
136+
| leet-check | [-i [题目编号]][-r] | 执行检测一个题目,不带参数检测今日题目,`-i` 参数指定题号去检测,`-r` 检测上一个随机出的题目 |
116137

117138
> note:
118-
> 名词解释: 题目编号或者题目名称(题目创建的时候的唯一标识,可以是题目的LeetCode编号也可以是自己起得别名)
139+
> 名词解释: 题目编号是leet-code为其分配的唯一id,如`1314`对应题目`矩阵区域和`
119140
120141
## Contributor
121142

common/resources/store.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"today":"2182","today-question-info":{"id":"82","enName":"remove-duplicates-from-sorted-list-ii","title":"删除排序链表中的重复元素 II","detail":"<p>给定一个已排序的链表的头&nbsp;<code>head</code> ,&nbsp;<em>删除原始链表中所有重复数字的节点,只留下不同的数字</em>&nbsp;。返回 <em>已排序的链表</em>&nbsp;。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/01/04/linkedlist1.jpg\" style=\"height: 142px; width: 500px;\" />\n<pre>\n<strong>输入:</strong>head = [1,2,3,3,4,4,5]\n<strong>输出:</strong>[1,2,5]\n</pre>\n\n<p><strong>示例 2:</strong></p>\n<img alt=\"\" src=\"https://assets.leetcode.com/uploads/2021/01/04/linkedlist2.jpg\" style=\"height: 164px; width: 400px;\" />\n<pre>\n<strong>输入:</strong>head = [1,1,1,2,3]\n<strong>输出:</strong>[2,3]\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li>链表中节点数目在范围 <code>[0, 300]</code> 内</li>\n\t<li><code>-100 &lt;= Node.val &lt;= 100</code></li>\n\t<li>题目数据保证链表已经按升序 <strong>排列</strong></li>\n</ul>\n","date":"2024-01-15","jsCode":"/**\n * Definition for singly-linked list.\n * function ListNode(val, next) {\n * this.val = (val===undefined ? 0 : val)\n * this.next = (next===undefined ? null : next)\n * }\n */\n/**\n * @param {ListNode} head\n * @return {ListNode}\n */\nvar deleteDuplicates = function(head) {\n\n};"},"today-tag":"82"}
1+
{"today":"2182","today-question-info":{"id":"2744","enName":"find-maximum-number-of-string-pairs","title":"最大字符串配对数目","detail":"<p>给你一个下标从 <strong>0</strong>&nbsp;开始的数组&nbsp;<code>words</code>&nbsp;,数组中包含 <strong>互不相同</strong>&nbsp;的字符串。</p>\n\n<p>如果字符串&nbsp;<code>words[i]</code>&nbsp;与字符串 <code>words[j]</code>&nbsp;满足以下条件,我们称它们可以匹配:</p>\n\n<ul>\n\t<li>字符串&nbsp;<code>words[i]</code>&nbsp;等于&nbsp;<code>words[j]</code>&nbsp;的反转字符串。</li>\n\t<li><code>0 &lt;= i &lt; j &lt; words.length</code></li>\n</ul>\n\n<p>请你返回数组 <code>words</code>&nbsp;中的&nbsp;<strong>最大</strong>&nbsp;匹配数目。</p>\n\n<p>注意,每个字符串最多匹配一次。</p>\n\n<p>&nbsp;</p>\n\n<p><strong>示例 1:</strong></p>\n\n<pre>\n<b>输入:</b>words = [\"cd\",\"ac\",\"dc\",\"ca\",\"zz\"]\n<b>输出:</b>2\n<strong>解释:</strong>在此示例中,我们可以通过以下方式匹配 2 对字符串:\n- 我们将第 0 个字符串与第 2 个字符串匹配,因为 word[0] 的反转字符串是 \"dc\" 并且等于 words[2]。\n- 我们将第 1 个字符串与第 3 个字符串匹配,因为 word[1] 的反转字符串是 \"ca\" 并且等于 words[3]。\n可以证明最多匹配数目是 2 。\n</pre>\n\n<p><strong>示例 2:</strong></p>\n\n<pre>\n<b>输入:</b>words = [\"ab\",\"ba\",\"cc\"]\n<b>输出:</b>1\n<b>解释:</b>在此示例中,我们可以通过以下方式匹配 1 对字符串:\n- 我们将第 0 个字符串与第 1 个字符串匹配,因为 words[1] 的反转字符串 \"ab\" 与 words[0] 相等。\n可以证明最多匹配数目是 1 。\n</pre>\n\n<p><strong>示例 3:</strong></p>\n\n<pre>\n<b>输入:</b>words = [\"aa\",\"ab\"]\n<b>输出:</b>0\n<strong>解释:</strong>这个例子中,无法匹配任何字符串。\n</pre>\n\n<p>&nbsp;</p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= words.length &lt;= 50</code></li>\n\t<li><code>words[i].length == 2</code></li>\n\t<li><code>words</code>&nbsp;包含的字符串互不相同。</li>\n\t<li><code>words[i]</code>&nbsp;只包含小写英文字母。</li>\n</ul>\n","date":"2024-01-17","jsCode":"/**\n * @param {string[]} words\n * @return {number}\n */\nvar maximumNumberOfStringPairs = function(words) {\n\n};"},"today-tag":"82"}

common/scripts/checkToday.js

-11
This file was deleted.

common/scripts/createToday.js

-18
This file was deleted.

common/utils/getTestCase.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
* @returns {string}
77
*/
88
function getTestCase(question){
9-
const cases = question.detail.match(/<strong><\/strong>.+\n/g).map(str=>str.replaceAll("<strong>输入:","").replaceAll("</strong>","").replace("\n",""))
10-
const expires = question.detail.match(/<strong><\/strong>.+\n/g).map(str=>str.replaceAll("<strong>输出:</strong>","").replace("\n",""))
11-
const functionName = question.jsCode.match(/var.+=/g)[0].replace("var ","").replace(" =","");
9+
console.log(question)
10+
const cases = question.detail.match(/(<[a-zA-Z]+><\/[a-zA-Z]+>).+\n/g)?.map(str=>`[${str?.replace(/<[a-zA-Z]+>/gm,"")?.replace(/<\/[a-zA-Z]+>/,"")?.replace("\n","")}]`)
11+
const expires = question.detail.match(/(<[a-zA-Z]+><\/[a-zA-Z]+>).+\n/g)?.map(str=>str?.replace(/<[a-zA-Z]+><\/[a-zA-Z]+>/gm,"")?.replace("\n",""))
12+
const functionName = question.jsCode?.match(/var.+=/g)?.[0]?.replace("var ","")?.replace(" =","");
1213
return `showLogs(
1314
${functionName},
1415
[${cases}],

common/utils/withTimeLog.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,19 @@ const v8 = require("v8");
22
const { getFileSize } = require("./sizeUtil");
33
const { isSameData } = require("./isSameData");
44

5+
/**
6+
* 执行并输出时间和内存
7+
* @param fnName
8+
* @param param 传入数组 全部通过解构来做
9+
* @param compare
10+
* @returns {{预期结果: string, 执行结果: string, 内存占用: string, 测试结果: (string), 执行用时: string}}
11+
*/
512
function withTimeLog(fnName, param, compare) {
613
// 记录开始时间
714
const startTime = performance.now();
815
// 获取函数执行前的内存使用情况
916
const startHeapStatsArray = v8.getHeapSpaceStatistics()
10-
const callVal = fnName(param)
17+
const callVal = fnName(...param)
1118
// 获取函数执行后的内存使用情况
1219
const endHeapStatsArray = v8.getHeapSpaceStatistics()
1320
// 记录结束时间

package.json

+1-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@
66
"type": "commonjs",
77
"scripts": {
88
"leet-check": "node common/scripts/check.js",
9-
"leet-create": "node common/scripts/create.js",
10-
"preget-question": "rimraf index.html",
11-
"get-question": "node common/question/getQuestion.js",
12-
"create-today": "node common/scripts/createToday.js",
13-
"check-today": "node common/scripts/checkToday.js"
9+
"leet-create": "node common/scripts/create.js"
1410
},
1511
"author": "EternalHeart",
1612
"license": "ISC",

resources/leet-check.png

40.8 KB
Loading

resources/leet-create-0.png

6.96 KB
Loading

resources/leet-create-1.png

219 KB
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { showLogs } = require("../../common/utils/withTimeLog");
2+
/**
3+
* 2744.最大字符串配对数目 [2024-01-17]
4+
* 给你一个下标从 0 开始的数组 words ,数组中包含 互不相同 的字符串。
5+
* 如果字符串 words[i] 与字符串 words[j] 满足以下条件,我们称它们可以匹配:
6+
* 字符串 words[i] 等于 words[j] 的反转字符串。
7+
* 0 <= i < j < words.length
8+
* 请你返回数组 words 中的 最大 匹配数目。
9+
* 注意,每个字符串最多匹配一次。
10+
* <img src="resources/leet-check.png" alt="图片" width="400" height="230"/>
11+
* 示例 1:
12+
* 输入:words = ["cd","ac","dc","ca","zz"]
13+
* 输出:2
14+
* 解释:在此示例中,我们可以通过以下方式匹配 2 对字符串:
15+
* - 我们将第 0 个字符串与第 2 个字符串匹配,因为 word[0] 的反转字符串是 "dc" 并且等于 words[2]。
16+
* - 我们将第 1 个字符串与第 3 个字符串匹配,因为 word[1] 的反转字符串是 "ca" 并且等于 words[3]。
17+
* 可以证明最多匹配数目是 2 。
18+
* 示例 2:
19+
* 输入:words = ["ab","ba","cc"]
20+
* 输出:1
21+
* 解释:在此示例中,我们可以通过以下方式匹配 1 对字符串:
22+
* - 我们将第 0 个字符串与第 1 个字符串匹配,因为 words[1] 的反转字符串 "ab" 与 words[0] 相等。
23+
* 可以证明最多匹配数目是 1 。
24+
* 示例 3:
25+
* 输入:words = ["aa","ab"]
26+
* 输出:0
27+
* 解释:这个例子中,无法匹配任何字符串。
28+
*
29+
* 提示:
30+
* 1 <= words.length <= 50
31+
* words[i].length == 2
32+
* words 包含的字符串互不相同。
33+
* words[i] 只包含小写英文字母。
34+
*
35+
*/
36+
/**
37+
* @param {string[]} words
38+
* @return {number}
39+
*/
40+
var maximumNumberOfStringPairs = function(words) {
41+
const n = words.length;
42+
let ans = 0;
43+
for (let i = 0; i < n; ++i) {
44+
for (let j = i + 1; j < n; ++j) {
45+
if (words[i][0] == words[j][1] && words[i][1] == words[j][0]) {
46+
++ans;
47+
}
48+
}
49+
}
50+
return ans;
51+
};
52+
53+
/**
54+
* Test case
55+
*/
56+
showLogs(
57+
maximumNumberOfStringPairs,
58+
[[words = ["cd","ac","dc","ca","zz"]],[words = ["ab","ba","cc"]],[words = ["aa","ab"]]],
59+
[2,1,0]
60+
)
61+
console.log("点击跳转到题目提交:https://leetcode.cn/problems/find-maximum-number-of-string-pairs/")
62+

0 commit comments

Comments
 (0)