Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Subscribe to see which companies asked this question
class Solution {
public:
string reverseString(string s) {
if(s == "") return s;
for(int i = 0, j = s.size()-1; i < j; i++, j--) {
char ch = s[i];
s[i] = s[j];
s[j] = ch;
}
return s;
}
}
本文介绍了一个简单的C++函数,该函数接收一个字符串作为输入,并返回其反转后的字符串。通过使用两个指针从两端向中间遍历并交换字符的方式实现。例如,输入hello将返回olleh。
710

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



