标签:字符串
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
本文出自 “做最好的自己” 博客,请务必保留此出处http://qiaopeng688.blog.51cto.com/3572484/1836488
leetCode 344. Reverse String 字符串
标签:字符串
原文地址:http://qiaopeng688.blog.51cto.com/3572484/1836488