leetcode-557. 反转字符串中的单词 III[python]
2018-10-10 15:20
429 查看
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
示例 1:
输入: “Let’s take LeetCode contest”
输出: “s’teL ekat edoCteeL tsetnoc”
注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。
class Solution(object): def reverseWords(self, s): """ :type s: str :rtype: str """ temp = s.split(' ')#split是将s中的字符串以' '为界进行分割 ans = [] for i in temp: ans.append(i[::-1]) return ' '.join(ans)#join是将ans列表里的字符串通过' '连接
先将单词依空格分块,分别用[::-1]反向输出后再用join()依空格拼接。
阅读更多相关文章推荐
- LeetCode 557. 反转字符串中的单词 III Python
- LeetCode 557. 反转字符串中的单词 III
- leetcode - 557 - 反转字符串中的单词 III
- LeetCode 557. Reverse Words in a String III (反转字符串中的单词 III)
- leetcode Reverse Words in a String III 反转字符串中的单词
- [leetcode, python] Reverse Words in a String 反转字符串
- LeetCode | Reverse Words in a String(字符串中的单词序反转)
- [leetcode, python] Reverse Words in a String 反转字符串
- [leetcode, python] Reverse Words in a String 反转字符串
- python---字符串的拼接、去重、反转、字母花样排序、单词出现判断、统计文件特定单词频率lambda、硬盘容量、列表转字符串
- [leetcode, python] Reverse Words in a String 反转字符串
- LeetCode 345. 反转字符串中的元音字母 Python
- [leetcode, python] Reverse Words in a String 反转字符串
- Leetcod:557.Reverse Words in a String III 反转字符串中的每个单词。
- 轻松python文本专题-字符串逐字符反转以及逐单词反转
- [leetcode, python] Reverse Words in a String 反转字符串
- [Leetcode,python] Reverse String 反转字符串
- LeetCode按单词反转字符串
- leetcode:557 反转字符串的单词
- 轻松python文本专题-字符串逐字符反转以及逐单词反转