LeetCode 第 344 题(Reverse String)
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
这道题非常简单。用 C++ 来写的话主要是考察对 string 类型的掌握情况。下面是代码:
string reverseString(string s)
{
string ret;
if(s.empty()) return ret;
string::const_iterator tail = s.cend();
do
{
tail --;
ret.push_back(*tail);
}while (tail != s.cbegin());
return ret;
}