MDGSF Software Engineer

[算法学习][leetcode] 344 Reverse String

2017-09-05
mdgsf
Art

https://leetcode.com/problems/reverse-string/description/

题目

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

Example:

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

题目翻译

题目解析

参考答案

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

class Solution {
public:
    string reverseString(string s)
    {
        int iStart = 0;
        int iEnd = s.size() - 1;
        while (iStart < iEnd)
        {
            vSwap(s[iStart], s[iEnd]);
            ++iStart;
            --iEnd;
        }
        return s;
    }

    void vSwap(char & a, char & b)
    {
        char temp = a;
        a = b;
        b = temp;
    }
};

int main()
{
    return 0;
}

weixingongzhonghao

Similar Posts

Comments