【Leetcode】Reverse String(easy)

来源:互联网 发布:宜兴淘宝照片拍摄 编辑:程序博客网 时间:2024/06/09 19:30

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;}
0 0
原创粉丝点击