344. Reverse String -- Python

344. Reverse String

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

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

代码:

class Solution:
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        # out=''
        # for i in range(len(s)):
        #     out+=s[len(s)-i-1]
        # return out
        return s[::-1]

两种方法就行,只是直接返回的肯定效率更高一点。

你可能感兴趣的:(leetcode)