Ai
1 Star 2 Fork 5

LilithSangreal/LeetCode-Solutions

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
ternary-expression-parser.cpp 864 Bytes
Copy Edit Raw Blame History
kamyu authored 2016-10-23 18:42 +08:00 . Update ternary-expression-parser.cpp
// Time: O(n)
// Space: O(1)
class Solution {
public:
string parseTernary(string expression) {
if (expression.empty()) {
return "";
}
string stack;
for (int i = expression.length() - 1; i >= 0; --i) {
auto c = expression[i];
if (!stack.empty() && stack.back() == '?') {
stack.pop_back(); // pop '?'
auto first = stack.back(); stack.pop_back();
stack.pop_back(); // pop ':'
auto second = stack.back(); stack.pop_back();
if (c == 'T') {
stack.push_back(first);
} else {
stack.push_back(second);
}
} else {
stack.push_back(c);
}
}
return string(1, stack.back());
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LilithSangreal/LeetCode-Solutions.git
git@gitee.com:LilithSangreal/LeetCode-Solutions.git
LilithSangreal
LeetCode-Solutions
LeetCode-Solutions
master

Search