Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
wwwanlingxiao
LeetCodeAnimation
Commits
84a8d074
Unverified
Commit
84a8d074
authored
Apr 29, 2020
by
程序员吴师兄
Committed by
GitHub
Apr 29, 2020
Browse files
Merge pull request #74 from xiaoshuai96/master
solved @xiaoshuai96
parents
c92c0649
f5e26604
Changes
2
Hide whitespace changes
Inline
Side-by-side
0058-length-Of-Last-Word/Animation/0058.gif
0 → 100644
View file @
84a8d074
1.19 MB
0058-length-Of-Last-Word/Article/0058-length-Of-Last-Word.md
0 → 100644
View file @
84a8d074
> 本文首发于公众号「图解面试算法」,是 [图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>) 系列文章之一。
>
> 个人博客:https://www.zhangxiaoshuai.fun
**本题选自leetcode第58题,easy难度,目前通过率33.0%**
### 题目描述:
```
txt
给定一个仅包含大小写字母和空格' '的字符串s,返回其最后一个单词的长度。
如果字符串从左向右滚动显示,那么最后一个单词就是最后出现的单词。
如果不存在最后一个单词,请返回0。
说明:一个单词是指仅由字母组成、不包含任何空格字符的最大子字符串。
示例:
输入:"Hello World"
输出:5
```
### 题目分析:
既然需要求出最后一个单词的长度,那我们直接从
**字符串的末尾**
开始好了;
这里末尾有两种情况:有空格和没有空格
```
(1)有空格:我们从末尾忽略掉空格,然后找到第一个遇见的字符(遇到第一个空格或者遍历完整个字符串为止)
(2)无空格:直接从末尾往前寻找即可(遇到第一个空格或者遍历完整个字符串为止)
```
### 动画gif演示:

### 代码:
**The first version**
```
java
public
int
lengthOfLastWord
(
String
s
)
{
if
(
s
.
length
()==
0
)
{
return
0
;
}
int
index
=
0
;
int
temp
=
0
;
int
p
=
s
.
length
()-
1
;
while
((
p
-
index
>=
0
)
&&
s
.
charAt
(
p
-
index
)
==
32
)
index
++;
for
(
int
i
=
p
-
index
;
i
>=
0
;
i
--)
{
if
(
s
.
charAt
(
i
)
!=
32
){
temp
++;
continue
;
}
break
;
}
return
temp
;
}
```
**2.代码:**
**The second version**
```
java
public
int
lengthOfLastWord
(
String
s
)
{
int
len
=
0
;
for
(
int
i
=
s
.
length
()
-
1
;
i
>=
0
;
i
--)
{
if
(
s
.
charAt
(
i
)
!=
' '
)
{
len
++;
}
else
if
(
len
!=
0
)
{
return
len
;
}
}
return
len
;
}
```
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment