Reverse String

问题描述

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

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

题目连接

https://leetcode.com/problems/reverse-string/

Python

使用python字符串强大的切片解决

class Solution(object):
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return s[::-1]

JavaScript

/**
* @param {string} s
* @return {string}
*/
var reverseString = function(s) {
return s.split("").reverse().join("")
};

Go

func reverseString(s string) string {
runes := []rune(s)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}

Java

public String reverseString(String s) {
return new StringBuilder(s).reverse().toString();
}