My answer,4 ms;breif;


  • 1
    M

    class Solution {

    public:

    int lengthOfLastWord(string s) {
      int length=s.size();
      int res=0;
      for(int i=length;i>0;--i){
          if(s[i-1]!=' '){
              res++;
          }
          else if(s[i-1]==' '&&res!=0){
              return res;
          }
      }
      return res;
    
    }
    

    };


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.