Skip to content

Commit 79a7e1b

Browse files
committed
feat: add solutions to lc problem: No.0058
No.0058.Length of Last Word
1 parent 3a9aa9a commit 79a7e1b

File tree

5 files changed

+102
-29
lines changed

5 files changed

+102
-29
lines changed

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

+35-11
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252

5353
**方法一:逆向遍历 + 双指针**
5454

55-
从字符串 $s$ 末尾开始遍历,找到第一个不为空格的字符,即为最后一个单词的最后一个字符,下标记为 $i$。然后继续向前遍历,找到第一个为空格的字符,即为最后一个单词的第一个字符的前一个字符,记为 $j$。那么最后一个单词的长度即为 $i - j$。
55+
我们从字符串 $s$ 末尾开始遍历,找到第一个不为空格的字符,即为最后一个单词的最后一个字符,下标记为 $i$。然后继续向前遍历,找到第一个为空格的字符,即为最后一个单词的第一个字符的前一个字符,记为 $j$。那么最后一个单词的长度即为 $i - j$。
5656

57-
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为字符串 $s$ 长度。
57+
时间复杂度 $O(n)$,其中 $n$ 为字符串 $s$ 长度。空间复杂度 $O(1)$
5858

5959
<!-- tabs:start -->
6060

@@ -100,10 +100,14 @@ class Solution {
100100
class Solution {
101101
public:
102102
int lengthOfLastWord(string s) {
103-
int i = s.length() - 1;
104-
while (i >= 0 && s[i] == ' ') --i;
103+
int i = s.size() - 1;
104+
while (~i && s[i] == ' ') {
105+
--i;
106+
}
105107
int j = i;
106-
while (j >= 0 && s[j] != ' ') --j;
108+
while (~j && s[j] != ' ') {
109+
--j;
110+
}
107111
return i - j;
108112
}
109113
};
@@ -149,13 +153,33 @@ var lengthOfLastWord = function (s) {
149153

150154
```ts
151155
function lengthOfLastWord(s: string): number {
152-
s = s.trimEnd();
153-
const n = s.length;
154-
const index = s.lastIndexOf(' ');
155-
if (index !== -1) {
156-
return n - index - 1;
156+
let i = s.length - 1;
157+
while (i >= 0 && s[i] === ' ') {
158+
--i;
159+
}
160+
let j = i;
161+
while (j >= 0 && s[j] !== ' ') {
162+
--j;
163+
}
164+
return i - j;
165+
}
166+
```
167+
168+
### **C#**
169+
170+
```cs
171+
public class Solution {
172+
public int LengthOfLastWord(string s) {
173+
int i = s.Length - 1;
174+
while (i >= 0 && s[i] == ' ') {
175+
--i;
176+
}
177+
int j = i;
178+
while (j >= 0 && s[j] != ' ') {
179+
--j;
180+
}
181+
return i - j;
157182
}
158-
return n;
159183
}
160184
```
161185

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

+39-9
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@
4444

4545
## Solutions
4646

47+
**Approach 1: Reverse traversal + two pointers**
48+
49+
We start traversing from the end of the string $s$, finding the last character of the last word, which is not a space, and the subscript is $i$. Then continue to traverse forward to find the first space character, which is the character before the first character of the last word, and mark it as $j$. Then the length of the last word is $i - j$.
50+
51+
Time complexity $O(n)$, where $n$ is the length of the string $s$. Space complexity $O(1)$.
52+
4753
<!-- tabs:start -->
4854

4955
### **Python3**
@@ -84,10 +90,14 @@ class Solution {
8490
class Solution {
8591
public:
8692
int lengthOfLastWord(string s) {
87-
int i = s.length() - 1;
88-
while (i >= 0 && s[i] == ' ') --i;
93+
int i = s.size() - 1;
94+
while (~i && s[i] == ' ') {
95+
--i;
96+
}
8997
int j = i;
90-
while (j >= 0 && s[j] != ' ') --j;
98+
while (~j && s[j] != ' ') {
99+
--j;
100+
}
91101
return i - j;
92102
}
93103
};
@@ -133,13 +143,33 @@ var lengthOfLastWord = function (s) {
133143

134144
```ts
135145
function lengthOfLastWord(s: string): number {
136-
s = s.trimEnd();
137-
const n = s.length;
138-
const index = s.lastIndexOf(' ');
139-
if (index !== -1) {
140-
return n - index - 1;
146+
let i = s.length - 1;
147+
while (i >= 0 && s[i] === ' ') {
148+
--i;
149+
}
150+
let j = i;
151+
while (j >= 0 && s[j] !== ' ') {
152+
--j;
153+
}
154+
return i - j;
155+
}
156+
```
157+
158+
### **C#**
159+
160+
```cs
161+
public class Solution {
162+
public int LengthOfLastWord(string s) {
163+
int i = s.Length - 1;
164+
while (i >= 0 && s[i] == ' ') {
165+
--i;
166+
}
167+
int j = i;
168+
while (j >= 0 && s[j] != ' ') {
169+
--j;
170+
}
171+
return i - j;
141172
}
142-
return n;
143173
}
144174
```
145175

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
class Solution {
22
public:
33
int lengthOfLastWord(string s) {
4-
int i = s.length() - 1;
5-
while (i >= 0 && s[i] == ' ') --i;
4+
int i = s.size() - 1;
5+
while (~i && s[i] == ' ') {
6+
--i;
7+
}
68
int j = i;
7-
while (j >= 0 && s[j] != ' ') --j;
9+
while (~j && s[j] != ' ') {
10+
--j;
11+
}
812
return i - j;
913
}
1014
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class Solution {
2+
public int LengthOfLastWord(string s) {
3+
int i = s.Length - 1;
4+
while (i >= 0 && s[i] == ' ') {
5+
--i;
6+
}
7+
int j = i;
8+
while (j >= 0 && s[j] != ' ') {
9+
--j;
10+
}
11+
return i - j;
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
function lengthOfLastWord(s: string): number {
2-
s = s.trimEnd();
3-
const n = s.length;
4-
const index = s.lastIndexOf(' ');
5-
if (index !== -1) {
6-
return n - index - 1;
2+
let i = s.length - 1;
3+
while (i >= 0 && s[i] === ' ') {
4+
--i;
75
}
8-
return n;
6+
let j = i;
7+
while (j >= 0 && s[j] !== ' ') {
8+
--j;
9+
}
10+
return i - j;
911
}

0 commit comments

Comments
 (0)