File tree 5 files changed +195
-2
lines changed
solution/1600-1699/1678.Goal Parser Interpretation
5 files changed +195
-2
lines changed Original file line number Diff line number Diff line change @@ -44,7 +44,6 @@ G -> G
44
44
<li><code>command</code> 由 <code>"G"</code>、<code>"()"</code> 和/或 <code>"(al)"</code> 按某种顺序组成</li>
45
45
</ul >
46
46
47
-
48
47
## 解法
49
48
50
49
<!-- 这里可写通用的实现逻辑 -->
@@ -56,7 +55,28 @@ G -> G
56
55
<!-- 这里可写当前语言的特殊实现逻辑 -->
57
56
58
57
``` python
58
+ class Solution :
59
+ def interpret (self , command : str ) -> str :
60
+ return command.replace(' ()' , ' o' ).replace(' (al)' , ' al' )
61
+ ```
59
62
63
+ ``` python
64
+ class Solution :
65
+ def interpret (self , command : str ) -> str :
66
+ res = ' '
67
+ i, n = 0 , len (command)
68
+ while i < n:
69
+ c = command[i]
70
+ if c == ' G' :
71
+ res += c
72
+ i += 1
73
+ elif c == ' (' and command[i + 1 ] != ' )' :
74
+ res += ' al'
75
+ i += 4
76
+ else :
77
+ res += ' o'
78
+ i += 2
79
+ return res
60
80
```
61
81
62
82
### ** Java**
@@ -89,6 +109,55 @@ class Solution {
89
109
}
90
110
```
91
111
112
+ ### ** C++**
113
+
114
+ ``` cpp
115
+ class Solution {
116
+ public:
117
+ string interpret(string command) {
118
+ string res = "";
119
+ int i = 0, n = command.size();
120
+ while (i < n) {
121
+ char c = command[ i] ;
122
+ if (c == 'G') {
123
+ res += "G";
124
+ i += 1;
125
+ } else if (c == '(' && command[ i + 1] != ')') {
126
+ res += "al";
127
+ i += 4;
128
+ } else {
129
+ res += "o";
130
+ i += 2;
131
+ }
132
+ }
133
+ return res;
134
+ }
135
+ };
136
+ ```
137
+
138
+ ### **Go**
139
+
140
+ ```go
141
+ func interpret(command string) string {
142
+ var res string
143
+ i, n := 0, len(command)
144
+ for i < n {
145
+ c := command[i]
146
+ if c == 'G' {
147
+ res += "G"
148
+ i += 1
149
+ } else if c == '(' && command[i+1] != ')' {
150
+ res += "al"
151
+ i += 4
152
+ } else {
153
+ res += "o"
154
+ i += 2
155
+ }
156
+ }
157
+ return res
158
+ }
159
+ ```
160
+
92
161
### ** ...**
93
162
94
163
```
Original file line number Diff line number Diff line change @@ -43,15 +43,35 @@ The final concatenated result is "Goal".
43
43
<li><code>command</code> consists of <code>"G"</code>, <code>"()"</code>, and/or <code>"(al)"</code> in some order.</li>
44
44
</ul >
45
45
46
-
47
46
## Solutions
48
47
49
48
<!-- tabs:start -->
50
49
51
50
### ** Python3**
52
51
53
52
``` python
53
+ class Solution :
54
+ def interpret (self , command : str ) -> str :
55
+ return command.replace(' ()' , ' o' ).replace(' (al)' , ' al' )
56
+ ```
54
57
58
+ ``` python
59
+ class Solution :
60
+ def interpret (self , command : str ) -> str :
61
+ res = ' '
62
+ i, n = 0 , len (command)
63
+ while i < n:
64
+ c = command[i]
65
+ if c == ' G' :
66
+ res += c
67
+ i += 1
68
+ elif c == ' (' and command[i + 1 ] != ' )' :
69
+ res += ' al'
70
+ i += 4
71
+ else :
72
+ res += ' o'
73
+ i += 2
74
+ return res
55
75
```
56
76
57
77
### ** Java**
@@ -82,6 +102,55 @@ class Solution {
82
102
}
83
103
```
84
104
105
+ ### ** C++**
106
+
107
+ ``` cpp
108
+ class Solution {
109
+ public:
110
+ string interpret(string command) {
111
+ string res = "";
112
+ int i = 0, n = command.size();
113
+ while (i < n) {
114
+ char c = command[ i] ;
115
+ if (c == 'G') {
116
+ res += "G";
117
+ i += 1;
118
+ } else if (c == '(' && command[ i + 1] != ')') {
119
+ res += "al";
120
+ i += 4;
121
+ } else {
122
+ res += "o";
123
+ i += 2;
124
+ }
125
+ }
126
+ return res;
127
+ }
128
+ };
129
+ ```
130
+
131
+ ### **Go**
132
+
133
+ ```go
134
+ func interpret(command string) string {
135
+ var res string
136
+ i, n := 0, len(command)
137
+ for i < n {
138
+ c := command[i]
139
+ if c == 'G' {
140
+ res += "G"
141
+ i += 1
142
+ } else if c == '(' && command[i+1] != ')' {
143
+ res += "al"
144
+ i += 4
145
+ } else {
146
+ res += "o"
147
+ i += 2
148
+ }
149
+ }
150
+ return res
151
+ }
152
+ ```
153
+
85
154
### ** ...**
86
155
87
156
```
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ string interpret (string command) {
4
+ string res = " " ;
5
+ int i = 0 , n = command.size ();
6
+ while (i < n) {
7
+ char c = command[i];
8
+ if (c == ' G' ) {
9
+ res += " G" ;
10
+ i += 1 ;
11
+ } else if (c == ' (' && command[i + 1 ] != ' )' ) {
12
+ res += " al" ;
13
+ i += 4 ;
14
+ } else {
15
+ res += " o" ;
16
+ i += 2 ;
17
+ }
18
+ }
19
+ return res;
20
+ }
21
+ };
Original file line number Diff line number Diff line change
1
+ func interpret (command string ) string {
2
+ var res string
3
+ i , n := 0 , len (command )
4
+ for i < n {
5
+ c := command [i ]
6
+ if c == 'G' {
7
+ res += "G"
8
+ i += 1
9
+ } else if c == '(' && command [i + 1 ] != ')' {
10
+ res += "al"
11
+ i += 4
12
+ } else {
13
+ res += "o"
14
+ i += 2
15
+ }
16
+ }
17
+ return res
18
+ }
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def interpret (self , command : str ) -> str :
3
+ res = ''
4
+ i , n = 0 , len (command )
5
+ while i < n :
6
+ c = command [i ]
7
+ if c == 'G' :
8
+ res += c
9
+ i += 1
10
+ elif c == '(' and command [i + 1 ] != ')' :
11
+ res += 'al'
12
+ i += 4
13
+ else :
14
+ res += 'o'
15
+ i += 2
16
+ return res
You can’t perform that action at this time.
0 commit comments