Skip to content

Commit bdfca0d

Browse files
author
lucifer
committed
feat: trie 模板,复制测试用例增加更多的情况,zen mode
1 parent 240f93c commit bdfca0d

File tree

7 files changed

+402
-19
lines changed

7 files changed

+402
-19
lines changed

leetcode-cheat.zip

-49.7 KB
Binary file not shown.

public/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"manifest_version": 2,
33
"name": "leetcode cheatsheet",
44
"description": "刷题小助手,made by 力扣加加",
5-
"version": "0.3.0",
5+
"version": "0.3.2",
66
"browser_action": {
77
"default_popup": "index.html",
88
"default_title": "力扣加加"

src/App.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ import {
1212
LEETCODE_URL,
1313
CONTRIBUTE_PROGRAMMING_LANGUAGE_URL,
1414
} from "./constant/index";
15-
import TestCase from "./testCase";
15+
// import TestCase from "./testCase";
1616
import ProblemDetail from "./Detail";
1717
import TagOrLink from "./TagOrLink";
1818
import tempaltes from "./codeTemplates/index";
1919
import Codes from "./codes";
20-
import { bfs } from "./utils";
20+
// import { bfs } from "./utils";
2121
// import drawTree from "canvas-binary-tree";
2222
import "antd/dist/antd.css";
2323
import "./App.css";
@@ -173,8 +173,6 @@ function App() {
173173
</Button>
174174
))}
175175

176-
<TestCase />
177-
178176
<div style={page === "" ? {} : { display: "none" }}>
179177
<h2 style={{ display: "flex", justifyContent: "center" }}>
180178
代码模板

src/codeTemplates/trie.js

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,207 @@
11
import treeLogo from "../imgs/tree.svg";
22

3+
const cppCode = `
4+
struct TrieNode {
5+
TrieNode *children[26];
6+
bool isEnd;
7+
TrieNode(bool end=false) {
8+
isEnd = end;
9+
memset(children, 0, sizeof(children));
10+
}
11+
};
12+
13+
class Trie {
14+
private:
15+
TrieNode *root;
16+
17+
TrieNode* findString(string word) {
18+
TrieNode *p = root;
19+
for (size_t i=0; i<word.size(); i++) {
20+
int index = word[i] - 'a';
21+
if(p->children[index] == nullptr)
22+
return nullptr;
23+
p = p->children[index];
24+
}
25+
return p;
26+
}
27+
28+
void clear(TrieNode *root) {
29+
for (size_t i = 0; i < 26; i ++)
30+
if (root->children[i])
31+
clear(root->children[i]);
32+
delete root;
33+
}
34+
public:
35+
/** Initialize your data structure here. */
36+
Trie() {
37+
root = new TrieNode();
38+
}
39+
40+
~Trie() {
41+
clear(root);
42+
}
43+
44+
/** Inserts a word into the trie. */
45+
void insert(string word) {
46+
TrieNode *p = root;
47+
for(size_t i=0; i<word.size(); i++) {
48+
int index = word[i] - 'a';
49+
if (!p->children[index])
50+
p->children[index] = new TrieNode();
51+
p = p->children[index];
52+
}
53+
p->isEnd = true;
54+
}
55+
56+
/** Returns if the word is in the trie. */
57+
bool search(string word) {
58+
TrieNode *p = findString(word);
59+
return p != nullptr && p->isEnd;
60+
}
61+
62+
/** Returns if there is any word in the trie that starts with the given prefix. */
63+
bool startsWith(string prefix) {
64+
TrieNode *p = findString(prefix);
65+
return p != nullptr;
66+
}
67+
};
68+
69+
/**
70+
* Your Trie object will be instantiated and called as such:
71+
* Trie* obj = new Trie();
72+
* obj->insert(word);
73+
* bool param_2 = obj->search(word);
74+
* bool param_3 = obj->startsWith(prefix);
75+
*/
76+
`;
77+
const goCode = `
78+
type Node struct {
79+
isWord bool
80+
next map[byte]*Node
81+
}
82+
83+
func NewNode(isWord bool) *Node {
84+
return &Node{isWord: isWord, next: make(map[byte]*Node)}
85+
}
86+
87+
type Trie struct {
88+
root *Node
89+
}
90+
91+
//func NewTrie() *Trie {
92+
// return &Trie{root: NewNode(false)}
93+
//}
94+
95+
/** Initialize your data structure here. */
96+
func Constructor() Trie {
97+
return Trie{root: NewNode(false)}
98+
}
99+
100+
101+
/** Inserts a word into the trie. */
102+
func (t *Trie) Insert(word string) {
103+
cur := t.root
104+
for i := 0; i < len(word); i++ {
105+
c := word[i]
106+
_, ok := cur.next[c]
107+
if !ok { //
108+
cur.next[c] = NewNode(false)
109+
}
110+
cur = cur.next[c]
111+
}
112+
if !cur.isWord { // 标记为单词
113+
cur.isWord = true
114+
}
115+
}
116+
117+
118+
/** Returns if the word is in the trie. */
119+
func (t *Trie) Search(word string) bool {
120+
cur := t.root
121+
for i := 0; i < len(word); i++ {
122+
c := word[i]
123+
v, ok := cur.next[c]
124+
if !ok {
125+
return false
126+
}
127+
cur = v
128+
}
129+
return cur.isWord
130+
}
131+
132+
133+
/** Returns if there is any word in the trie that starts with the given prefix. */
134+
func (t *Trie) StartsWith(prefix string) bool {
135+
cur := t.root
136+
for i := 0; i < len(prefix); i++ {
137+
c := prefix[i]
138+
v,ok := cur.next[c]
139+
if !ok {
140+
return false
141+
}
142+
cur = v
143+
}
144+
return true
145+
}
146+
`;
147+
148+
const javaCode = `
149+
class Trie {
150+
151+
class TireNode {
152+
boolean isEnd = false;
153+
TireNode[] next = new TireNode[26];
154+
TireNode() {}
155+
}
156+
157+
private TireNode root;
158+
159+
/** Initialize your data structure here. */
160+
public Trie() {
161+
root = new TireNode();
162+
}
163+
164+
/** Inserts a word into the trie. */
165+
public void insert(String word) {
166+
TireNode node = root;
167+
for (char ch : word.toCharArray()) {
168+
if (node.next[ch-'a'] == null) {
169+
node.next[ch-'a'] = new TireNode();
170+
}
171+
node = node.next[ch-'a'];
172+
}
173+
node.isEnd = true;
174+
}
175+
176+
/** Returns if the word is in the trie. */
177+
public boolean search(String word) {
178+
TireNode node = root;
179+
for (char ch : word.toCharArray()) {
180+
if (node.next[ch-'a'] == null) return false;
181+
node = node.next[ch-'a'];
182+
}
183+
return node.isEnd;
184+
}
185+
186+
/** Returns if there is any word in the trie that starts with the given prefix. */
187+
public boolean startsWith(String prefix) {
188+
TireNode node = root;
189+
for (char ch : prefix.toCharArray()) {
190+
if (node.next[ch-'a'] == null) return false;
191+
node = node.next[ch-'a'];
192+
}
193+
return true;
194+
}
195+
}
196+
197+
/**
198+
* Your Trie object will be instantiated and called as such:
199+
* Trie obj = new Trie();
200+
* obj.insert(word);
201+
* boolean param_2 = obj.search(word);
202+
* boolean param_3 = obj.startsWith(prefix);
203+
*/
204+
`;
3205
export default {
4206
title: "前缀树",
5207
logo: treeLogo,
@@ -160,6 +362,18 @@ class Trie:
160362
*/
161363
`,
162364
},
365+
{
366+
language: "cpp",
367+
text: cppCode,
368+
},
369+
{
370+
language: "go",
371+
text: goCode,
372+
},
373+
{
374+
language: "java",
375+
text: javaCode,
376+
},
163377
],
164378
},
165379
],

src/contentScript.js

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import { message } from "antd";
22
// import "./content.css";
33
import { copyToClipboard, 不讲武德 } from "./utils";
4+
import zenAble from "./zen/zenMode";
5+
6+
// if (testCase[i] === '"') {
7+
// while (i < testCase.length && testCase[i] !== '"') {
8+
// stack.push(testCase[i]);
9+
// }
10+
// stack.push("\n");
11+
// } else
412

513
// testcase eg: `bottom = "BCD", allowed = ["BCG", "CDE", "GEA", "FFF"], c = [1,2,3], d = 2`
614
function normalize(testCase) {
7-
testCase = testCase.replace(/\n/g, "").replace("&nbsp;", "");
8-
console.log(testCase);
15+
testCase = testCase.trim().replace(/\n/g, "").replace("&nbsp;", "");
16+
917
// 单一参数
18+
// console.log(testCase);
1019
if (!testCase.includes("=")) {
1120
// 数组直接返回
12-
if (testCase.includes("[")) {
21+
if (testCase.includes("[") || testCase.includes('"')) {
1322
return testCase;
1423
} else {
1524
// 输入: 3, 2, 0, 0
@@ -58,6 +67,14 @@ function normalize(testCase) {
5867
return stack.join("");
5968
}
6069

70+
function extractTestCase(text, prefix) {
71+
const testCase = text.match(new RegExp(`${prefix}(.*)输出`, "s"));
72+
if (!testCase || testCase.length <= 1) {
73+
return text.match(new RegExp(`${prefix}(.*)$`, "s"));
74+
}
75+
return testCase;
76+
}
77+
6178
function getProviedTestCases() {
6279
const possibleTags = ["pre", "p"];
6380
const possiblePrefixs = ["输入:", "输入:"];
@@ -68,12 +85,10 @@ function getProviedTestCases() {
6885
for (let prefix of possiblePrefixs) {
6986
for (var i = 0; i < pres.length; ++i) {
7087
if (pres[i].innerText.includes(prefix)) {
71-
const testcase = pres[i].innerText.match(
72-
new RegExp(`${prefix}(.*)输出`, "s")
73-
);
74-
console.log(testcase);
75-
if (testcase.length <= 1) {
76-
return 不讲武德();
88+
const testcase = extractTestCase(pres[i].innerText, prefix);
89+
if (!testcase || testcase.length <= 1) {
90+
不讲武德();
91+
return [];
7792
}
7893
ans.push(normalize(testcase[1]));
7994
}
@@ -100,15 +115,20 @@ function insertButton() {
100115
});
101116
};
102117
buttons[i].parentElement.prepend(copyButton);
103-
break;
118+
return true;
104119
}
105120
}
121+
return false;
106122
}
107123
let inserted = false;
108124
const timerId = setInterval(() => {
109125
if (inserted) return clearInterval(timerId);
110-
insertButton();
111-
inserted = true;
126+
if (insertButton()) {
127+
window.location.title = "";
128+
inserted = true;
129+
// 可进入禅定模式
130+
zenAble();
131+
}
112132
}, 1000);
113133

114134
// class Main extends React.Component {

src/testCase.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,10 @@ export default function TestCase() {
252252
type="primary"
253253
onClick={() => {
254254
t().then((elements) => {
255-
alert(JSON.stringify(elements));
256255
const cases = getProviedTestCases(elements);
257256
const ans = cases.map(normalize).join("\n");
258257
console.log(cases, ans);
259-
alert(elements);
258+
260259
if (ans) {
261260
if (copyToClipboard(ans)) message.success("复制成功");
262261
}

0 commit comments

Comments
 (0)