내가 작성한 코드
reverse(s.begin(), s.end());
string str, ans;
for (auto iter = s.begin(); iter != s.end(); iter++)
{
char ch = *iter;
if (ch == ' ')
{
if (str == "")
continue;
reverse(str.begin(), str.end());
ans += str + ' ';
str = "";
}
else
str += ch;
}
reverse(str.begin(), str.end());
if (str == "")
ans.pop_back();
return ans + str;
인터넷에서 퍼온 코드
stringstream st(s);
string word;
vector<string> ans;
while (st >> word)
ans.emplace_back(word);
// Reverse the vector
std::reverse(ans.begin(), ans.end());
string result = "";
for (int i = 0; i < ans.size() - 1; ++i)
result += ans[i] + " ";
return result + ans[ans.size() - 1];
'코딩테스트 > LeetCode' 카테고리의 다른 글
[LeetCode] 45. Jump Game II (0) | 2024.06.17 |
---|---|
[LeetCode] 55. Jump Game (0) | 2024.06.17 |
[LeetCode] 136. Single Number (0) | 2024.06.13 |
[LeetCode] 1 - Two Sum (0) | 2024.04.03 |
[LeetCode] 6 - Zigzag Conversion (0) | 2024.04.03 |