# [58\. Length of Last Word](https://leetcode.com/problems/length-of-last-word/) :::spoiler Solution ```cpp= class Solution { public: int lengthOfLastWord(string s) { int n = s.size(); int res = 0; int i = n - 1; // 如果遇到空字串,移動指標 while (i >= 0 && s[i] == ' ') { --i; } // 如果遇到單字,移動指標 while (i >= 0 && s[i] != ' ') { --i; ++res; } // 最後回傳 res return res; } }; ``` - T: $O(N)$ - S: $O(1)$ :::