简单题,我不知道为啥我做的这么纠结..........
就是扫描...有空位做个标记,没有就长度就涨...
如果之前有空位了,再次出现非空格,那么从0算....
class Solution { public: int lengthOfLastWord(const char *s) { int len = 0; int state = 0; while(*s){ if(*s != ' ') { if(state == 0) len++; else { state = 0; len = 0; len++; } } if(*s == ' '){ if(state == 0){ state = 1; } } s++; } return len; } };
by 1957