A fantastic error...


  • 0
    S

    Submission Result: Wrong AnswerMore Details

    Input: " "

    Output: ""

    Expected: ""

    The output and expected are really the same thing. I wonder how the system judge the code? Thanks!!

    code if you are interested,

       void reverse(std::string &s, int start, int end){
               for(;start<end;++start,--end){
                   char temp=s[end];
                   end=start;
                   start=temp;
               }
        }
        void reverseWords(string &s) {
            if(s.size()==0) return; 
    
           // remove duplicate white spaces
            int index1=0, index2=0;
            for(bool firstSpace=false;index1<s.size();){
                if(s[index1]!=' '){
                    firstSpace=true;
                    s[index2++]=s[index1++];
                }else if(s[index1]==' '&&firstSpace){
                    firstSpace=false;
                    s[index2++]=s[index1++];
                }else{
                    ++index1;
                }
            }
            s[index2]='\0';
        
           // reverse each word 
            int index=0;
            int startOfAWord=index;
            for(;index<s.size();++index){
                if(s[index]==' '&&s[index-1]!=' '){
                    reverse(s, startOfAWord, index-1);
                }else if(s[index]!=' '&&index>startOfAWord&&s[index-1]==' '){
                    startOfAWord=index;
                }else if(index==s.size()-1&&s[index]!=' '){
                    reverse(s,startOfAWord, index);
                }
            }
    
           //reverse the string
            reverse(s,0,s.size()-1);
        }

Log in to reply
 

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