题目考察的知识点:字符串的遍历
题目解答方法的文字分析:从后往前遍历即可。
本题解析所用的编程语言:c++
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param s string字符串 * @return int整型 */ int lengthOfLastWord(string s) { // write code here int i = s.size() - 1; while (s[i] == ' ') --i; int j = i; while (j >=0 && s[j] != ' ') { --j; } return i - j; } };