1957

无聊蛋疼的1957写的低端博客
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

[leetcode]Length of Last Word

Posted on 2014-01-03 13:19  1957  阅读(121)  评论(0编辑  收藏  举报

简单题,我不知道为啥我做的这么纠结..........

 

就是扫描...有空位做个标记,没有就长度就涨...

如果之前有空位了,再次出现非空格,那么从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;
    }
};