Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 119c372

Browse files
✨feat: add 1678
1 parent c747151 commit 119c372

File tree

2 files changed

+118
-0
lines changed

2 files changed

+118
-0
lines changed

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@
189189
| [1656. 设计有序流](https://leetcode.cn/problems/design-an-ordered-stream/) | [LeetCode 题解链接](https://leetcode.cn/problems/design-an-ordered-stream/solution/by-ac_oier-5pe8/) | 简单 | 🤩🤩🤩🤩 |
190190
| [1662. 检查两个字符串数组是否相等](https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-two-string-arrays-are-equivalent/solution/by-ac_oier-h0l6/) | 简单 | 🤩🤩🤩🤩 |
191191
| [1672. 最富有客户的资产总量](https://leetcode-cn.com/problems/richest-customer-wealth/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/richest-customer-wealth/solution/by-ac_oier-ai19/) | 简单 | 🤩🤩🤩🤩 |
192+
| [1678. 设计 Goal 解析器](https://leetcode.cn/problems/goal-parser-interpretation/) | [LeetCode 题解链接](https://leetcode.cn/problems/goal-parser-interpretation/solution/by-ac_oier-a00y/) | 简单 | 🤩🤩🤩🤩 |
192193
| [1688. 比赛中的配对次数](https://leetcode-cn.com/problems/count-of-matches-in-tournament/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-of-matches-in-tournament/solution/gong-shui-san-xie-jian-dan-nao-jin-ji-zh-cx7a/) | 简单 | 🤩🤩🤩🤩🤩 |
193194
| [1694. 重新格式化电话号码](https://leetcode.cn/problems/reformat-phone-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/reformat-phone-number/solution/by-ac_oier-82xq/) | 简单 | 🤩🤩🤩🤩 |
194195
| [1700. 无法吃午餐的学生数量](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/) | [LeetCode 题解链接](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/solution/by-ac_oier-rvc3/) | 简单 | 🤩🤩🤩🤩🤩 |
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1678. 设计 Goal 解析器](https://leetcode.cn/problems/goal-parser-interpretation/solution/by-ac_oier-a00y/)** ,难度为 **简单**
4+
5+
Tag : 「模拟」
6+
7+
8+
9+
请你设计一个可以解释字符串 `command``Goal` 解析器 。
10+
11+
`command``"G"``"()"``"(al)"` 按某种顺序组成。`Goal` 解析器会将 `"G"` 解释为字符串 `"G"``"()"` 解释为字符串 `"o"` ,`"(al)"` 解释为字符串 `"al"`
12+
13+
然后,按原顺序将经解释得到的字符串连接成一个字符串。
14+
15+
给你字符串 `command`,返回 `Goal` 解析器 对 `command` 的解释结果。
16+
17+
示例 1:
18+
```
19+
输入:command = "G()(al)"
20+
21+
输出:"Goal"
22+
23+
解释:Goal 解析器解释命令的步骤如下所示:
24+
G -> G
25+
() -> o
26+
(al) -> al
27+
最后连接得到的结果是 "Goal"
28+
```
29+
示例 2:
30+
```
31+
输入:command = "G()()()()(al)"
32+
33+
输出:"Gooooal"
34+
```
35+
示例 3:
36+
```
37+
输入:command = "(al)G(al)()()G"
38+
39+
输出:"alGalooG"
40+
```
41+
42+
提示:
43+
* 1ドル <= command.length <= 100$
44+
* `command``"G"``"()"``"(al)"` 按某种顺序组成
45+
46+
---
47+
48+
### 模拟
49+
50+
根据题意进行模拟即可。
51+
52+
Java 代码:
53+
```Java
54+
class Solution {
55+
public String interpret(String s) {
56+
StringBuilder sb = new StringBuilder();
57+
int n = s.length();
58+
for (int i = 0; i < n; ) {
59+
if (s.charAt(i) == 'G') {
60+
sb.append('G'); i++;
61+
} else if (i + 1 < n && s.charAt(i + 1) == ')') {
62+
sb.append('o'); i += 2;
63+
} else {
64+
sb.append("al"); i += 4;
65+
}
66+
}
67+
return sb.toString();
68+
}
69+
}
70+
```
71+
TypeScript 代码:
72+
```TypeScript
73+
function interpret(s: string): string {
74+
const n = s.length
75+
let ans = ''
76+
for (let i = 0; i < n; ) {
77+
if (s[i] == 'G') {
78+
ans += 'G'; i++
79+
} else if (i + 1 < n && s[i + 1] == ')') {
80+
ans += 'o'; i += 2
81+
} else {
82+
ans += 'al'; i += 4
83+
}
84+
}
85+
return ans
86+
}
87+
```
88+
Python 代码:
89+
```Python
90+
class Solution:
91+
def interpret(self, s: str) -> str:
92+
ans = ''
93+
n, i = len(s), 0
94+
while i < n:
95+
if s[i] == 'G':
96+
ans, i = ans + s[i], i + 1
97+
elif i + 1 < n and s[i + 1] == ')':
98+
ans, i = ans + 'o', i + 2
99+
else:
100+
ans, i = ans + 'al', i + 4
101+
return ans
102+
```
103+
* 时间复杂度:$O(n)$
104+
* 空间复杂度:$O(n)$
105+
106+
---
107+
108+
### 最后
109+
110+
这是我们「刷穿 LeetCode」系列文章的第 `No.1678` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
111+
112+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
113+
114+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
115+
116+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
117+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /