美文网首页
344. Reverse String

344. Reverse String

作者: SetsunaChiya | 来源:发表于2016-11-11 22:42 被阅读0次

344. Reverse String

Python:

最Pythonic的解法咯

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

Discuss有人问如下解法为什么会报Time Limit Exceeded
python字符串是不可变对象。每次+=操作都要创建新对象并赋值给res。OJ会给很长的字符串作为输入,所以就悲剧了。应该是用join()
(忘了在哪里看过,join好像是一起拼接还是什么)

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

相关文章

  • 2019-04-08

    LeetCode 344. Reverse String Description Write a function...

  • 344. Reverse String

    344. Reverse String Easy Write a function that reverses a...

  • 344. 反转字符串

    344. 反转字符串[https://leetcode.cn/problems/reverse-string/] ...

  • 344. Reverse String

    344. Reverse String Python: 最Pythonic的解法咯 Discuss有人问如下解法为...

  • String

    唐博主刷了String,我那周太忙了,需要慢慢自己补啦。344. Reverse String : Easy3....

  • 344. Reverse String

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

  • 344. Reverse String

    Leetcode Day 1 344 Reverse String 题目:Write a function tha...

  • 344. Reverse String

    Problem Write a function that takes a string as input and...

  • 344. Reverse String

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

  • 344. Reverse String

    1.描述 Write a function that takes a string as input and re...

网友评论

      本文标题:344. Reverse String

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