Example:
Given s = "hello", return "olleh".
Subscribe to see which companies asked this question
class Solution {
public:
string reverseString(string s) {
int len = s.length();
string r;
for(int i=0; i<len; i++)
{
//r.push_back(s[len-1-i]); //+ and push_back() function are both OK
r += s[len-1-i];
//r.append(s[len-1-i]);
}
return r;
}
};

本文提供了一种使用C++实现字符串反转的方法。通过遍历输入字符串并从后向前构建新字符串的方式,实现了字符串的反转功能。该方法简单高效,适用于多种应用场景。
599

被折叠的 条评论
为什么被折叠?



