方法:最简单的是从后向前遍历即可
class Solution { public: int lengthOfLastWord(string s) { int len = 0, i=s.size()-1; while(i>=0 && s[i] == ‘ ‘) --i; while(i>=0 && s[i--] != ‘ ‘) ++len; return len; } };
时间: 2024-08-25 21:05:22
方法:最简单的是从后向前遍历即可
class Solution { public: int lengthOfLastWord(string s) { int len = 0, i=s.size()-1; while(i>=0 && s[i] == ‘ ‘) --i; while(i>=0 && s[i--] != ‘ ‘) ++len; return len; } };