重点1.循环初始值注意不能越界。如果直接取字符串长度则下标会溢出所以要减1.2.循环条件注意等于号i 0,等号不成立的话会遗漏没有空格的情况3.循环中判断条件考虑字符串最后面为空格的情况。class Solution { public: int lengthOfLastWord(string s) { int j0; for(int i s.size()-1;i0;i--){ if(s[i] ){ if(j0) return j; continue; }j; }return j; } };class Solution { public int lengthOfLastWord(String s) { int j0; for(int is.length()-1;i0;i--){ char es.charAt(i); if(e j0){ return j; }else if(e! ) j; }return j; } }