Problem Description
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
Java:
class Solution {
public String reverseString(String s) {
return new StringBuilder(s).reverse().toString();
}
}
Python:
class Solution:
def reverseString(self, s):
"""
:type s: str
:rtype: str
"""
return s[::-1]
Go:
/*
rune 類型是 Unicode 字符類型,和 int32 類型等價(jià),通常用于表示一個(gè) Unicode 碼點(diǎn)。rune 和 int32 可以互換使用。
byte 是uint8類型的等價(jià)類型,byte類型一般用于強(qiáng)調(diào)數(shù)值是一個(gè)原始的數(shù)據(jù)而不是 一個(gè)小的整數(shù)。
uintptr 是一種無符號(hào)的整數(shù)類型,沒有指定具體的bit大小但是足以容納指針。 uintptr類型只有在底層編程是才需要,特別是Go語言和C語言函數(shù)庫或操作系統(tǒng)接口相交互的地方。
*/
func reverseString(s string) string {
runes := []rune(s)
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}
return string(runes)
}