卖萌的弱渣

I am stupid, I am hungry.

Reverse String

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

Example

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

Solution

  • Java
(Reverse-String.java) download
1
2
3
4
5
public class Solution {
    public String reverseString(String s) {
        return new StringBuilder(s).reverse().toString();
    }
}
  • Python
(Reverse-String.py) download
1
2
3
4
5
6
7
8
class Solution(object):
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """

        return "".join(reversed(s))