LeetCode.344.Reverse String

原題鏈接:Reverse String

題目內容:
Write a function that takes a string as input and returns the string reversed.

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


Python

class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        a = [letter for letter in s]
        a.reverse()
        return "".join(a)

C++

class Solution {
public:
    string reverseString(string s) {
        for(int i=0,j=s.size()-1;i<j;i++,j--){  
            char c=s[i];  
            s[i]=s[j];  
            s[j]=c;  
        }  
        return s;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章