leetcode python反转字符串中的单词

# Leetcode 557 反转字符串中的单词III
### 题目描述

给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。

**示例1:**

输入: "Let's take LeetCode contest"
输出: "s'teL ekat edoCteeL tsetnoc"

class Solution:
    def reverseWords(self, s: str) -> str:
        ls = s.split()
        for i,s in enumerate(ls):
            ls[i] = s[::-1]
        return " ".join(ls)

enumerate():可以同时列出 可遍历的数据对象(如列表、元组或字符串)的下标和数据。

猜你喜欢

转载自www.cnblogs.com/hooo-1102/p/11060275.html