344. Reverse String
Write a function that takes a string as input and returns the string reversed.
Example
Given s=”hello”, return “olleh”.
C++:
class Solution{
public:
string reverseString(string s){
std::string sr(s.rbegin(), s.rend());
return sr;
}
};
Top Solution
class Solution{
public:
string reverseString(string s){
int i=0, j=s.size()-1;
while(i<j)
swap(s[i++], s[j--]);
return s;
}
};
本文介绍了一种使用C++实现的字符串反转方法。通过两种不同的技术方案实现了字符串的反转:一种是利用标准库中的反向迭代器;另一种是通过双指针技巧进行字符交换。这两种方法都有效地实现了字符串的反转。
736

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



