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

 

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

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

 

相关文章:

  • 2021-07-19
  • 2022-12-23
  • 2022-12-23
  • 2021-11-30
  • 2021-08-21
猜你喜欢
  • 2021-12-24
  • 2021-05-20
  • 2021-07-11
  • 2022-02-05
  • 2022-03-09
相关资源
相似解决方案