Skip to content

Commit 14652cd

Browse files
committed
feat: add solutions to lc problem: No.0058
No.0058.Length of Last Word
1 parent 46ed9a7 commit 14652cd

File tree

4 files changed

+134
-15
lines changed

4 files changed

+134
-15
lines changed

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

+61-5
Original file line numberDiff line numberDiff line change
@@ -95,21 +95,77 @@ class Solution {
9595
}
9696
```
9797

98+
### **Go**
99+
100+
```go
101+
func lengthOfLastWord(s string) int {
102+
if len(s) == 0 {
103+
return 0
104+
}
105+
space := []byte(" ")[0]
106+
for len(s) != 0 && s[len(s)-1] == space {
107+
s = s[:len(s)-1]
108+
}
109+
ret := 0
110+
for i := len(s) - 1; i >= 0; i-- {
111+
if s[i] != space {
112+
ret++
113+
} else {
114+
return ret
115+
}
116+
}
117+
return ret
118+
}
119+
```
120+
121+
### **JavaScript**
122+
123+
```js
124+
var lengthOfLastWord = function (s) {
125+
s = s.trim();
126+
return s.length - s.lastIndexOf(' ') - 1;
127+
};
128+
129+
var lengthOfLastWord2 = function (s) {
130+
let res = 0;
131+
for (let i = 0; i < s.length; i++) {
132+
if (s[i] !== ' ' && (i === 0 || s[i - 1] === ' ')) {
133+
res = 1;
134+
} else if (s[i] !== ' ') {
135+
res++;
136+
}
137+
}
138+
return res;
139+
};
140+
```
141+
142+
### **TypeScript**
143+
144+
```ts
145+
function lengthOfLastWord(s: string): number {
146+
s = s.trimEnd();
147+
const n = s.length;
148+
const index = s.lastIndexOf(' ');
149+
if (index !== -1) {
150+
return n - index - 1;
151+
}
152+
return n;
153+
}
154+
```
155+
98156
### **Rust**
99157

100158
```rust
101159
impl Solution {
102160
pub fn length_of_last_word(s: String) -> i32 {
103161
let s = s.trim_end();
104-
if s.len() == 0 {
105-
return 0;
106-
}
162+
let n = s.len();
107163
for (i, c) in s.char_indices().rev() {
108164
if c == ' ' {
109-
return (s.len() - i - 1) as i32;
165+
return (n - i - 1) as i32;
110166
}
111167
}
112-
s.len() as i32
168+
n as i32
113169
}
114170
}
115171
```

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

+61-5
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,77 @@ class Solution {
8585
}
8686
```
8787

88+
### **Go**
89+
90+
```go
91+
func lengthOfLastWord(s string) int {
92+
if len(s) == 0 {
93+
return 0
94+
}
95+
space := []byte(" ")[0]
96+
for len(s) != 0 && s[len(s)-1] == space {
97+
s = s[:len(s)-1]
98+
}
99+
ret := 0
100+
for i := len(s) - 1; i >= 0; i-- {
101+
if s[i] != space {
102+
ret++
103+
} else {
104+
return ret
105+
}
106+
}
107+
return ret
108+
}
109+
```
110+
111+
### **JavaScript**
112+
113+
```js
114+
var lengthOfLastWord = function (s) {
115+
s = s.trim();
116+
return s.length - s.lastIndexOf(' ') - 1;
117+
};
118+
119+
var lengthOfLastWord2 = function (s) {
120+
let res = 0;
121+
for (let i = 0; i < s.length; i++) {
122+
if (s[i] !== ' ' && (i === 0 || s[i - 1] === ' ')) {
123+
res = 1;
124+
} else if (s[i] !== ' ') {
125+
res++;
126+
}
127+
}
128+
return res;
129+
};
130+
```
131+
132+
### **TypeScript**
133+
134+
```ts
135+
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;
141+
}
142+
return n;
143+
}
144+
```
145+
88146
### **Rust**
89147

90148
```rust
91149
impl Solution {
92150
pub fn length_of_last_word(s: String) -> i32 {
93151
let s = s.trim_end();
94-
if s.len() == 0 {
95-
return 0;
96-
}
152+
let n = s.len();
97153
for (i, c) in s.char_indices().rev() {
98154
if c == ' ' {
99-
return (s.len() - i - 1) as i32;
155+
return (n - i - 1) as i32;
100156
}
101157
}
102-
s.len() as i32
158+
n as i32
103159
}
104160
}
105161
```
+3-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
impl Solution {
22
pub fn length_of_last_word(s: String) -> i32 {
33
let s = s.trim_end();
4-
if s.len() == 0 {
5-
return 0;
6-
}
4+
let n = s.len();
75
for (i, c) in s.char_indices().rev() {
86
if c == ' ' {
9-
return (s.len() - i - 1) as i32;
7+
return (n - i - 1) as i32;
108
}
119
}
12-
s.len() as i32
10+
n as i32
1311
}
1412
}
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
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;
7+
}
8+
return n;
9+
}

0 commit comments

Comments
 (0)