题目
https://leetcode-cn.com/problems/reverse-words-in-a-string-iii/
给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序。
示例 1:
输入: "Let's take LeetCode contest"
输出: "s'teL ekat edoCteeL tsetnoc"
注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格。
我的AC
class Solution(object):
def reverseWords(self, s):
"""
:type s: str
:rtype: str
"""
length = len(s)
word_list = s.split()
rev_list = [word[::-1] for word in word_list]
rev_str = " ".join(rev_list)
return rev_str
小结
s.split() # 字符串分割
rev_str = " ".join(rev_list) # 列表元素拼接为字符串
完整的一个 code
class Solution:
def intersect(self, nums1, nums2):
"""
:type nums1: List[int]
:type nums2: List[int]
:rtype: List[int]
"""
result = []
for i in nums1:
for j in nums2:
if i == j:
result.append(i) # 插入交集
nums2.remove(j) # 插入过的值不再出现
break
return result
if __name__ == "__main__":
nums1 = [1,2,2,1]
nums2 = [1,2]
s = Solution()
print(s.intersect(nums1, nums2))
原文:https://blog.csdn.net/qq_36190978/article/details/87286512
网友评论