您的位置:首页 > 其它

【Leetcode】Reverse String(easy)

2016-05-19 15:27 246 查看

Question

Write a function that takes a string as input and returns the string reversed.

Example:

Given s = “hello”, return “olleh”.

Code

#include<iostream>
#include<string>
using namespace std;

class Solution {
public:
string reverseString(string s) {
char temp;//临时变量
for (int i = 0; i < s.length() / 2; i++) {
temp = s[i];
s[i] = s[s.length() - 1 - i];
s[s.length() - 1 - i] = temp;
}
return s;
}
};

int main() {
Solution so;
string s = { "hello" };
cout << so.reverseString(s) << endl;;
system("pause");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: