Reverse Words in a String III

    这道题属于简单题

  题目:

    

  思路:

    1、我的思路比较不好,弄出来的代码不够简洁。我采用栈的形式,遍历整个列表,遇到‘ ’或者长度达到了最大(长度这个条件是为了防止示例中没有出现空格,只有一个单词)就把a[]当中的元素全部加到b[]中并添加一个‘ ’,最后去掉b的最后一个元素,再返回‘’。join(b)

    2、大神:只用了一行,他先用spilt将括号去除,再反向排序,再使用‘ ’.join(),最后再反向排序

  代码:

    1、我的垃圾代码:

 1 class Solution(object):
 2     def reverseWords(self, s):
 3         """
 4         :type s: str
 5         :rtype: str
 6         """
 7         a = []
 8         b = []
 9         for i in range(0, len(s) + 1):
10             if i == len(s) or s[i] == ‘ ‘:
11                 l = len(a)
12                 while l > 0:
13                     b.append(a.pop())
14                     l -= 1
15                 b.append(‘ ‘)
16             else: a.append(s[i])
17         b.pop()
18
19         return ‘‘.join(b)

    2、大神代码:

  

1 def reverseWords(self, s):
2     return ‘ ‘.join(s.split()[::-1])[::-1]
时间: 2024-08-06 03:44:33

Reverse Words in a String III的相关文章

557. Reverse Words in a String III【easy】

557. Reverse Words in a String III[easy] 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:

Leetcode#557. Reverse Words in a String III(反转字符串中的单词 III)

题目描述 给定一个字符串,你需要反转字符串中每个单词的字符顺序,同时仍保留空格和单词的初始顺序. 示例 1: 输入: "Let's take LeetCode contest" 输出: "s'teL ekat edoCteeL tsetnoc" 注意:在字符串中,每个单词由单个空格分隔,并且字符串中不会有任何额外的空格. 思路 分割字符串,再逆序,拼接到字符串 代码实现 package String; /** * 557. Reverse Words in a St

ledecode Reverse Words in a String III

557. Reverse Words in a String III 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: "

【leetcode】557. Reverse Words in a String III

Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-in-a-string-iii/ 1)problem Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace

557. Reverse Words in a String III

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"

leetcode_557 Reverse Words in a String III(String)

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"

LeetCode 557. Reverse Words in a String III

晚饭后写了一题.当然,我这种菜鸟是从easy开始写的...发现leetcode也是有bug的 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" O

Leetcode - 557. Reverse Words in a String III (C++) stringstream

1. 题目:https://leetcode.com/problems/reverse-words-in-a-string-iii/discuss/ 反转字符串中的所有单词. 2. 思路: 这题主要是要注意空格的影响.比方说,string首尾和单词之间可能有一或多个空格.看到有人逐个对空格判断,但是我觉得逐个判断其实稍微容易出错(当然如果非常熟悉的话就完全无所谓啦),我的一个简单想法是用stringstream. PS:不熟悉stringstream的朋友可以看看链接的文档. 3. 代码 cla

LeetCode Reverse Words in a String III

原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: 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: Inpu

LeetCode解题思路:557. Reverse Words in a String III

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"