版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51315232
返回字符串的逆序
原文
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = “hello”, return “olleh”.
翻译
写一个函数,以一个字符串作为输入,并逆序返回。
例如:
给定s = “hello”,返回”olleh”。
代码
直接逆序遍历
string reverseString(string s) {
string result;
for (int i = s.length() - 1; i >= 0; i--) {
result += s[i];
}
return result;
}
使用STL
string reverseString(string s) {
reverse(s.begin(), s.end());
return s;
}
Copyright
Nomasp by Ke Yuwang is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Permanent Link:nomasp.com , ALL RIGHTS RESERVED.