You have a string s that consists of English letters, punctuation marks, whitespace characters, and brackets. It is guaranteed that the parentheses in s form a regular bracket sequence. Your task is to reverse the strings contained in each pair of matching parentheses, starting from the innermost pair. The results string should not contain any parentheses. Example For string s = "a(bc)de", the output should be reverseParentheses(s) = "acbde". |
후위 연산자처럼 스택 사용 하려다가 망한 소스
code>>
std::string reverseParentheses(std::string s) {
std::string _reverse = "";
std::string output = "";
int check = 0;
for (int i = 0; i<s.size(); i++)
{
if (s[i] == '(')
{
_reverse += s[i];
check++;
continue;
}
else if (s[i] == ')')
{
std::reverse(_reverse.begin() + _reverse.find_last_of('('), _reverse.end());
_reverse.erase(_reverse.end() - 1);
check--;
if (check == 0)
{
output += _reverse;
_reverse = "";
}
continue;
}
if (_reverse.find('(') != std::string::npos)
{
_reverse += s[i];
}
else {
output += s[i];
}
}
return output;
}
'알고리즘 > codefights' 카테고리의 다른 글
15>addBorder (0) | 2017.06.28 |
---|---|
14>alternatingSums (0) | 2017.06.28 |
12>sortByHeight (0) | 2017.06.28 |
11>isLucky (0) | 2017.06.28 |
10>commonCharacterCount (0) | 2017.06.28 |