Skip to content

Commit f6d30f2

Browse files
committed
58. Length of Last Word
1 parent 71a0406 commit f6d30f2

File tree

4 files changed

+120
-0
lines changed

4 files changed

+120
-0
lines changed

markdown/58. Length of Last Word.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
### [58\. Length of Last Word](https://leetcode.com/problems/length-of-last-word/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given a string _s_ consists of upper/lower-case alphabets and empty space characters `' '`, return the length of last word in the string.
7+
8+
If the last word does not exist, return 0.
9+
10+
**Note:** A word is defined as a character sequence consists of non-space characters only.
11+
12+
**Example:**
13+
14+
```
15+
Input: "Hello World"
16+
Output: 5
17+
```
18+
19+
20+
#### Solution
21+
22+
Language: **Java**
23+
24+
```java
25+
class Solution {
26+
   public int lengthOfLastWord(String s) {
27+
       if (s == null) {
28+
           return 0;
29+
      }
30+
       s = s.trim();
31+
       char[] chars = s.toCharArray();
32+
       int length = 0;
33+
       for (int i = chars.length - 1; i >= 0; i--) {
34+
           if (chars[i] != ' ') {
35+
               length++;
36+
          } else {
37+
               break;
38+
          }
39+
      }
40+
       return length;
41+
  }
42+
}
43+
```
44+
![](http://ww1.sinaimg.cn/large/006tNc79ly1g4xg3lcobsj31bk0s2gqt.jpg)

src/main/java/leetcode/_58_/Main.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package leetcode._58_;
2+
3+
/**
4+
* Created by zhangbo54 on 2019-03-04.
5+
*/
6+
public class Main {
7+
public static void main(String[] args) {
8+
Solution solution = new Solution();
9+
System.out.println(solution.lengthOfLastWord("a"));
10+
}
11+
}
12+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package leetcode._58_;
2+
3+
class Solution {
4+
public int lengthOfLastWord(String s) {
5+
if (s == null) {
6+
return 0;
7+
}
8+
s = s.trim();
9+
char[] chars = s.toCharArray();
10+
int length = 0;
11+
for (int i = chars.length - 1; i >= 0; i--) {
12+
if (chars[i] != ' ') {
13+
length++;
14+
} else {
15+
break;
16+
}
17+
}
18+
return length;
19+
}
20+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
### [58\. Length of Last Word](https://leetcode.com/problems/length-of-last-word/)
2+
3+
Difficulty: **Easy**
4+
5+
6+
Given a string _s_ consists of upper/lower-case alphabets and empty space characters `' '`, return the length of last word in the string.
7+
8+
If the last word does not exist, return 0.
9+
10+
**Note:** A word is defined as a character sequence consists of non-space characters only.
11+
12+
**Example:**
13+
14+
```
15+
Input: "Hello World"
16+
Output: 5
17+
```
18+
19+
20+
#### Solution
21+
22+
Language: **Java**
23+
24+
```java
25+
class Solution {
26+
   public int lengthOfLastWord(String s) {
27+
       if (s == null) {
28+
           return 0;
29+
      }
30+
       s = s.trim();
31+
       char[] chars = s.toCharArray();
32+
       int length = 0;
33+
       for (int i = chars.length - 1; i >= 0; i--) {
34+
           if (chars[i] != ' ') {
35+
               length++;
36+
          } else {
37+
               break;
38+
          }
39+
      }
40+
       return length;
41+
  }
42+
}
43+
```
44+
![](http://ww1.sinaimg.cn/large/006tNc79ly1g4xg3lcobsj31bk0s2gqt.jpg)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy