Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3970415

Browse files
committed
feat: update solutions to leetcode problem: No.0058
1 parent e91e547 commit 3970415

File tree

4 files changed

+83
-6
lines changed

4 files changed

+83
-6
lines changed

‎solution/0000-0099/0058.Length of Last Word/README.md‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,42 @@
3030
<!-- 这里可写当前语言的特殊实现逻辑 -->
3131

3232
```python
33-
33+
class Solution:
34+
def lengthOfLastWord(self, s: str) -> int:
35+
last_word_length = 0
36+
meet_word = False
37+
for i in range(len(s) - 1, -1, -1):
38+
ch = ord(s[i])
39+
if ch >= 65 and ch <= 122:
40+
meet_word = True
41+
last_word_length += 1
42+
elif meet_word:
43+
break
44+
return last_word_length
3445
```
3546

3647
### **Java**
3748

3849
<!-- 这里可写当前语言的特殊实现逻辑 -->
3950

4051
```java
41-
52+
class Solution {
53+
public int lengthOfLastWord(String s) {
54+
int n = s.length();
55+
int lastWordLength = 0;
56+
boolean meetWord = false;
57+
for (int i = n - 1; i >= 0; --i) {
58+
char ch = s.charAt(i);
59+
if (ch >= 'A' && ch <= 'z') {
60+
meetWord = true;
61+
++lastWordLength;
62+
} else if (meetWord) {
63+
break;
64+
}
65+
}
66+
return lastWordLength;
67+
}
68+
}
4269
```
4370

4471
### **...**

‎solution/0000-0099/0058.Length of Last Word/README_EN.md‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,40 @@
2626
### **Python3**
2727

2828
```python
29-
29+
class Solution:
30+
def lengthOfLastWord(self, s: str) -> int:
31+
last_word_length = 0
32+
meet_word = False
33+
for i in range(len(s) - 1, -1, -1):
34+
ch = ord(s[i])
35+
if ch >= 65 and ch <= 122:
36+
meet_word = True
37+
last_word_length += 1
38+
elif meet_word:
39+
break
40+
return last_word_length
3041
```
3142

3243
### **Java**
3344

3445
```java
35-
46+
class Solution {
47+
public int lengthOfLastWord(String s) {
48+
int n = s.length();
49+
int lastWordLength = 0;
50+
boolean meetWord = false;
51+
for (int i = n - 1; i >= 0; --i) {
52+
char ch = s.charAt(i);
53+
if (ch >= 'A' && ch <= 'z') {
54+
meetWord = true;
55+
++lastWordLength;
56+
} else if (meetWord) {
57+
break;
58+
}
59+
}
60+
return lastWordLength;
61+
}
62+
}
3663
```
3764

3865
### **...**
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
class Solution {
22
public int lengthOfLastWord(String s) {
3-
s = s.trim();
4-
return s.length()-s.lastIndexOf(" ")-1;
3+
int n = s.length();
4+
int lastWordLength = 0;
5+
boolean meetWord = false;
6+
for (int i = n - 1; i >= 0; --i) {
7+
char ch = s.charAt(i);
8+
if (ch >= 'A' && ch <= 'z') {
9+
meetWord = true;
10+
++lastWordLength;
11+
} else if (meetWord) {
12+
break;
13+
}
14+
}
15+
return lastWordLength;
516
}
617
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def lengthOfLastWord(self, s: str) -> int:
3+
last_word_length = 0
4+
meet_word = False
5+
for i in range(len(s) - 1, -1, -1):
6+
ch = ord(s[i])
7+
if ch >= 65 and ch <= 122:
8+
meet_word = True
9+
last_word_length += 1
10+
elif meet_word:
11+
break
12+
return last_word_length

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /