Skip to content

Commit 84874cd

Browse files
committed
feat: 添加多语言代码片段定义(Java/Python/Go/JavaScript)
1 parent 90da347 commit 84874cd

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed

src/utils/codeSnippets.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
import type { CodeLanguage } from '../types'
2+
3+
export interface CodeSnippet {
4+
language: CodeLanguage
5+
code: string
6+
// 将通用行号映射到该语言的行号
7+
lineMapping: Record<number, number>
8+
}
9+
10+
// 通用行号定义(用于步骤生成器)
11+
export const LINE_NUMBERS = {
12+
FUNCTION_START: 1,
13+
CREATE_MAP: 2,
14+
FOR_LOOP: 3,
15+
CALC_COMPLEMENT: 4,
16+
CHECK_MAP: 5,
17+
RETURN_RESULT: 6,
18+
END_IF: 7,
19+
PUT_MAP: 8,
20+
END_FOR: 9,
21+
RETURN_EMPTY: 10,
22+
FUNCTION_END: 11,
23+
}
24+
25+
export const CODE_SNIPPETS: Record<CodeLanguage, CodeSnippet> = {
26+
java: {
27+
language: 'java',
28+
code: `public int[] twoSum(int[] nums, int target) {
29+
Map<Integer, Integer> map = new HashMap<>();
30+
for (int i = 0; i < nums.length; i++) {
31+
int complement = target - nums[i];
32+
if (map.containsKey(complement)) {
33+
return new int[] { map.get(complement), i };
34+
}
35+
map.put(nums[i], i);
36+
}
37+
return new int[] {};
38+
}`,
39+
lineMapping: {
40+
1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11,
41+
},
42+
},
43+
python: {
44+
language: 'python',
45+
code: `def twoSum(nums: List[int], target: int) -> List[int]:
46+
hash_map = {}
47+
for i, num in enumerate(nums):
48+
complement = target - num
49+
if complement in hash_map:
50+
return [hash_map[complement], i]
51+
hash_map[num] = i
52+
return []`,
53+
lineMapping: {
54+
1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 7, 9: 8, 10: 8, 11: 8,
55+
},
56+
},
57+
golang: {
58+
language: 'golang',
59+
code: `func twoSum(nums []int, target int) []int {
60+
hashMap := make(map[int]int)
61+
for i, num := range nums {
62+
complement := target - num
63+
if j, ok := hashMap[complement]; ok {
64+
return []int{j, i}
65+
}
66+
hashMap[num] = i
67+
}
68+
return []int{}
69+
}`,
70+
lineMapping: {
71+
1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11,
72+
},
73+
},
74+
javascript: {
75+
language: 'javascript',
76+
code: `function twoSum(nums, target) {
77+
const map = new Map();
78+
for (let i = 0; i < nums.length; i++) {
79+
const complement = target - nums[i];
80+
if (map.has(complement)) {
81+
return [map.get(complement), i];
82+
}
83+
map.set(nums[i], i);
84+
}
85+
return [];
86+
}`,
87+
lineMapping: {
88+
1: 1, 2: 2, 3: 3, 4: 4, 5: 5, 6: 6, 7: 7, 8: 8, 9: 9, 10: 10, 11: 11,
89+
},
90+
},
91+
}
92+
93+
export const LANGUAGE_LABELS: Record<CodeLanguage, string> = {
94+
java: 'Java',
95+
python: 'Python',
96+
golang: 'Go',
97+
javascript: 'JavaScript',
98+
}
99+
100+
export const LANGUAGE_ICONS: Record<CodeLanguage, string> = {
101+
java: '☕',
102+
python: '🐍',
103+
golang: '🐹',
104+
javascript: '📜',
105+
}
106+
107+
/**
108+
* 获取指定语言的代码
109+
*/
110+
export function getCodeForLanguage(language: CodeLanguage): string {
111+
return CODE_SNIPPETS[language].code
112+
}
113+
114+
/**
115+
* 将通用行号转换为指定语言的行号
116+
*/
117+
export function mapLineToLanguage(genericLine: number, language: CodeLanguage): number {
118+
return CODE_SNIPPETS[language].lineMapping[genericLine] || genericLine
119+
}

0 commit comments

Comments
 (0)