題目要求:
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
題目大意:
輸入一個(gè)字符串,將其按倒序輸出。
例子:輸入s='hello', 則輸出'olleh'。
解題思路:
利用string字符串?dāng)?shù)組找到中間字符,將前后字符依次交換位置,遍歷輸出。
C++代碼:
class Solution {
public:
? ?string reverseString(string s) {
? ? ? ?int i, n;
? ? ? ?char temp;
? ? ? ?n=s.size();
? ? ? ?for(i=0; i<n/2; i++)
? ? ? ?{
? ? ? ? ? ?temp=s[i];
? ? ? ? ? ?s[i] = s[n-i-1];
? ? ? ? ? ?str[n-i-1] = temp;
? ? ? ?}
? ? ? ?return s;
? ?}
};
因?yàn)檫€沒(méi)學(xué)會(huì)使用Markdown,所以現(xiàn)在的代碼只能這樣丑丑的放在這里了。