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 1bc952d

Browse files
feat: add rust solution to lc problem: No.0058.Length of Last Word
1 parent e7c07af commit 1bc952d

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,25 @@ class Solution {
8585
}
8686
```
8787

88+
### **Rust**
89+
90+
```rust
91+
impl Solution {
92+
pub fn length_of_last_word(s: String) -> i32 {
93+
let s = s.trim_end();
94+
if s.len() == 0 {
95+
return 0;
96+
}
97+
for (i, c) in s.char_indices().rev() {
98+
if c == ' ' {
99+
return (s.len() - i - 1) as i32;
100+
}
101+
}
102+
s.len() as i32
103+
}
104+
}
105+
```
106+
88107
### **...**
89108

90109
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
impl Solution {
2+
pub fn length_of_last_word(s: String) -> i32 {
3+
let s = s.trim_end();
4+
if s.len() == 0 {
5+
return 0;
6+
}
7+
for (i, c) in s.char_indices().rev() {
8+
if c == ' ' {
9+
return (s.len() - i - 1) as i32;
10+
}
11+
}
12+
s.len() as i32
13+
}
14+
}

0 commit comments

Comments
(0)

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