-
Notifications
You must be signed in to change notification settings - Fork 3
/
439.cpp
34 lines (32 loc) · 834 Bytes
/
439.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
public:
string parser(string& s, int& index) {
char select = s[index];
index += 2;
// true select
string trueStr;
if (s[index + 1] == '?') {
trueStr = parser(s, index);
index++; // skip :
}
else {
trueStr = s[index];
index += 2; // skip self + :
}
// false select
string falseStr;
if (index + 1 < s.size() && s[index + 1] == '?') {
falseStr = parser(s, index);
}
else {
falseStr = s[index];
index += 1; // skip self
}
if (select == 'T') return trueStr;
else return falseStr;
}
string parseTernary(string expression) {
int index = 0;
return parser(expression, index);
}
};