From 8d403277c220190d1c5e5403a7a4999dda86395b Mon Sep 17 00:00:00 2001 From: SquidSpirit Date: Mon, 17 Feb 2025 23:24:16 +0800 Subject: [PATCH] feat: 58_length_of_last_word --- 58_length_of_last_word/main.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 58_length_of_last_word/main.cpp diff --git a/58_length_of_last_word/main.cpp b/58_length_of_last_word/main.cpp new file mode 100644 index 0000000..b16ee85 --- /dev/null +++ b/58_length_of_last_word/main.cpp @@ -0,0 +1,14 @@ +#include +using namespace std; + +class Solution { + public: + int lengthOfLastWord(string s) { + int i = s.length() - 1, result = 0; + + while (s[i] == ' ') i--; + for (; i >= 0 && s[i] != ' '; i--, result++); + + return result; + } +};