Skip to content

Commit 2fa28f1

Browse files
authored
Merge pull request gzc426#231 from libihan/libihan-patch-1
Update libh_leetcode557 and leetcode58
2 parents ec54c32 + 2d5beb1 commit 2fa28f1

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

2018.12.2-leetcode557/李碧涵.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
```

2018.12.2-leetcode58/李碧涵.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
```

0 commit comments

Comments
 (0)