leecode 解题总结:344. Reverse String

#include 
#include 
#include 
#include 
using namespace std;
/*
问题:
Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

分析:逆置一个字符串

输入:
hello
hell
输出:
olleh
lleh
*/

class Solution {
public:
    string reverseString(string s) {
        if(s.empty())
		{
			return "";
		}
		int len = s.length();
		char temp;
		for(int i = 0 ; i < len / 2;  i++)
		{
			temp = s.at(i);
			s.at(i) = s.at(len - i - 1);
			s.at(len - i - 1) = temp;
		}
		return s;
    }
};


void process()
{
	 string value;
	 Solution solution;
	 while(cin >> value )
	 {
		 string result = solution.reverseString(value);
		 cout << result << endl;
	 }
}

int main(int argc , char* argv[])
{
	process();
	getchar();
	return 0;
}


你可能感兴趣的:(leecode)