LeetCode:557. Reverse Words in a String III

来源:互联网 发布:故宫淘宝与故宫旗舰店 编辑:程序博客网 时间:2024/06/15 16:07

Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Example 1:

Input: "Let's take LeetCode contest"Output: "s'teL ekat edoCteeL tsetnoc"

Note: In the string, each word is separated by single space and there will not be any extra space in the string.

最近做深度学习,涉及Python的学习,于是在LEETCODE上用Python解题感觉代码很简单,而且该有的功能都有,感觉会不会打代码会不会越来越退化。

AC:

class Solution(object):    def reverseWords(self, s):        """        :type s: str        :rtype: str        """        return ' '.join(x[::-1] for x in s.split())


原创粉丝点击