File tree Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Expand file tree Collapse file tree 2 files changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ #### [ 557. Reverse Words in a String III] ( https://leetcode.com/problems/reverse-words-in-a-string-iii/ )
2
+ ** 题目描述**
3
+ > 给定字符串,翻转字符串中每个单词的字符,保留空白和初始的单词顺序。
4
+
5
+ > 字符串中,单词间用空格分隔,且没有额外的空格。
6
+
7
+ ** 例子**
8
+ > Input: "Let's take LeetCode contest"
9
+ Output: "s'teL ekat edoCteeL tsetnoc"
10
+
11
+ ** 思想**
12
+ 首先split出每个单词。
13
+ ** 解法**
14
+ ``` python
15
+ class Solution (object ):
16
+ def reverseWords (self , s ):
17
+ """
18
+ :type s: str
19
+ :rtype: str
20
+ """
21
+ return ' ' .join(map (lambda x :x[::- 1 ], s.split()))
22
+ # return ' '.join(s.split()[::-1])[::-1]
23
+ # return ' '.join(s[::-1].split()[::-1])
24
+ ```
Original file line number Diff line number Diff line change
1
+ #### [ 58. Length of Last Word] ( https://leetcode.com/problems/length-of-last-word/ )
2
+ ** 题目描述**
3
+ > 给定一个只包含大小写字母或空字符的字符串s,返回字符串中最后一个单词的长度。如果最后一个单词不存在,返回0。
4
+
5
+ > 单词 - 只包含非空格字符的字符序列。
6
+
7
+ ** 例子**
8
+ > Input: "Hello World"
9
+ Output: 5
10
+
11
+ ** 思想**
12
+ 比较简单。注意:当s只包含空格时的情况
13
+ ** 解法**
14
+ ``` python
15
+ class Solution (object ):
16
+ def lengthOfLastWord (self , s ):
17
+ """
18
+ :type s: str
19
+ :rtype: int
20
+ """
21
+ s = s.split()
22
+ if not s:
23
+ return 0
24
+ return len (s[- 1 ])
25
+ ```
You can’t perform that action at this time.
0 commit comments