TreeviewCopyright © aleen42 all right reserved, powered by aleen42

557. 反转字符串中的单词 III

https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/

Java

/*
 * @Author: Goog Tech
 * @Date: 2020-08-09 07:28:06
 * @LastEditTime: 2020-08-09 07:33:49
 * @Description: https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/
 * @FilePath: \leetcode-googtech\#557. Reverse Words in a String III\Solution.java
 */

class Solution {
    public String reverseWords(String s) {
        // 去除字符串中的空格并将其转化为字符数组
        String[] str = s.split(" ");
        StringBuffer buffer = new StringBuffer();
        // 遍历数组中的字符串元素
        for(int i = 0; i < str.length; i++) {
            // 将其反转后添加到stringbuffer中
            buffer.append(new StringBuffer(str[i]).reverse().toString());
            // 并且每遍历一个字符串元素后就在stringbuffer后添加一个空格
            buffer.append(" ");
        }
        // 删除字符串中的头尾空格后返回
        return buffer.toString().trim();
    }
}

Python

'''
Author: Goog Tech
Date: 2020-08-09 07:28:12
LastEditTime: 2020-08-09 07:34:54
Description: https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/
FilePath: \leetcode-googtech\#557. Reverse Words in a String III\Solution.py
'''

class Solution(object):
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        # 将字符串分割成单词列表,然后把每个单词反转切片
        return " ".join(word[::-1] for word in s.split(" "))
Copyright © GoogTech 2021 all right reserved,powered by GitbookLast update time : 2021-09-15 01:55:05

results matching ""

    No results matching ""