344. Reverse String
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
思路1:
使用一个新的string来存放结果。
class Solution {
public:
string reverseString(string s) {
int len = s.size();
string result;
for(int n = 0; n < len; n++)
{
result.append(1,s.at(len - 1 - n));
}
return result;
}
};
思路2:
修改原来string直接得到结果。
class Solution {
public:
string reverseString(string s) {
int len = s.size();
for (int i = 0; i < len / 2 ; i++)
{
char a = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = a;
}
return s;
}
};
2016-08-10 13:04:05
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。