美文网首页
Leetcode 344 - Reverse String

Leetcode 344 - Reverse String

作者: 张桢_Attix | 来源:发表于2018-07-16 00:08 被阅读0次

    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 类型等价,通常用于表示一个 Unicode 码点。rune 和 int32 可以互换使用。
    
      byte 是uint8类型的等价类型,byte类型一般用于强调数值是一个原始的数据而不是 一个小的整数。
    
      uintptr 是一种无符号的整数类型,没有指定具体的bit大小但是足以容纳指针。 uintptr类型只有在底层编程是才需要,特别是Go语言和C语言函数库或操作系统接口相交互的地方。
    */
    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)
    }
    
    

    相关文章

      网友评论

          本文标题:Leetcode 344 - Reverse String

          本文链接:https://www.haomeiwen.com/subject/whmmpftx.html