Skip to content

Commit 5a85599

Browse files
Create 0058-length-of-last-word.scala
1 parent 138fe15 commit 5a85599

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Diff for: scala/0058-length-of-last-word.scala

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
object Solution {
2+
def lengthOfLastWord(s: String): Int = {
3+
var i = s.length - 1
4+
var length = 0
5+
6+
while (s(i) == ' ') {
7+
i -= 1
8+
}
9+
10+
while (i >= 0 && s(i) != ' ') {
11+
length += 1
12+
i -= 1
13+
}
14+
15+
length
16+
17+
}
18+
}

0 commit comments

Comments
 (0)