6 ms Java solution without string functions


  • 0

    ''' int l=0;
    int j=0;
    int i=0;
    boolean flag=true;
    i=s.length()-1;
    l=i+1;
    if(s.length()==0)
    return 0;

       while(flag){
         while((int)s.charAt(i)==32){
         if(i>0)
         i=i-1;
         else
         break;
         }
         j=i;
         
         while((int)s.charAt(i)!=32){
         if(i>0)
         i=i-1;
         else
         break;
         }
         
         if(i>0 && (int)s.charAt(i)==32 || i==0 && (int)s.charAt(i)==32){
         l=j-i;
         flag=false;
         }
         
         if(i==0 && (int)s.charAt(i)!=32){
         l=j-i+1;
         flag=false;
         }
          }  
       return l;

Log in to reply
 

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