From d93b8dc503e7ebb97a5e00c1b1b3d083ee34cb77 Mon Sep 17 00:00:00 2001 From: ductnn Date: Mon, 1 Apr 2024 22:19:21 +0700 Subject: [PATCH] more sol --- leetcode/58.LengthofLastWord/lengthOfLastWord.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/leetcode/58.LengthofLastWord/lengthOfLastWord.go b/leetcode/58.LengthofLastWord/lengthOfLastWord.go index 7c8cea3..3b11536 100644 --- a/leetcode/58.LengthofLastWord/lengthOfLastWord.go +++ b/leetcode/58.LengthofLastWord/lengthOfLastWord.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "strings" ) func lengthOfLastWord(s string) int { @@ -18,7 +19,14 @@ func lengthOfLastWord(s string) int { return i - j } +func lengthOfLastWord1(s string) int { + s = strings.TrimSpace(s) + words := strings.Split(s, " ") + + return len(words[len(words)-1]) +} + func main() { s := "luffy is still joyboy" - fmt.Println(lengthOfLastWord(s)) + fmt.Println(lengthOfLastWord1(s)) }