the question is about leetcode 344. Reverse String
class Solution {
public:
string reverseString(string s) {
char *array = new char[s.length()];
for(int i=0;i<s.length();i++)
{
array[i] =s[s.length()-i-1];
}
string strlist(array);
printf("%s\n",strlist.c_str());
return strlist;
}
};
input is "Sore was I ere I saw Eros." output is ".sorE was I ere I saw eroSnam A\""
it seems that my array has more element than I expected, and I have no idea how to avoid that, anyone can suggest that?
'\0'.new char[]? Especially when you're already usingstd::stringeverywhere?string reverseString(string s) { std::reverse(s.begin(), s.end()); return s; }?