Reverse Words in a String ---- Output Limit Exceeded


  • 0
    Y

    pleae help me!! Why I get an "OLE" result?

    class Solution {
    public:
    void reverseWords(string &s) {

    string ans="",t="";
    int i=0,j=0; 
    bool flag=false;      
    
    string tmp=s+" "; 
    s=tmp;                     
    for(i=0;i<s.length();)
    {                  
       if(s[i]==' ')   
       {
          if(!flag)
          {
             i++;
          }
          else
          {
    
              t=s.substr(j,i-j);
              t=t+" "+ans;
              ans=t;
              flag=false;
              i++;          
          } 
       }        
       else 
       {
           if(!flag)   j=i,flag=true;  
           i++; 
       }                         
    }
    ans=ans.substr(0,ans.length()-1);
    cout <<ans; 
    ans="";
    }
    

    };


  • 0
    S

    Do not cout <<ans; . https://oj.leetcode.com/faq/


  • 0
    Y

    thanks a lot ! I had passed it!


Log in to reply
 

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