[input:'';output:'';expected:'']; I can't see why it is wrong answer.


  • 0
    L

    my solution in C++ seems to be right but got wrong answer on this test case.

    input:'';
    output:'';
    expected:'';

    class Solution {
    public:
     // leetcode.cpp : Defines the entry point for the console application.
    //
        void reverseWords(string &s) {
    	    int len = s.length();
    	    int *pos = new int[len];
        	int n = 0;
    	    bool isWord = false;
    	    int p = 0;
    	    while (s[p] != '\0'){
    		    if( (s[p] > 'a' - 1) &&( s[p]<'z' + 1) ||( s[p]>'A')&&(s[p] < 'Z')){
    			    if (!isWord){
    				    isWord = true;
    				    pos[2*n] = p;
    			    }
    		    }
    		    else{
    			    if (isWord){
    				    pos[2*n + 1] = p - 1;
    				    n++;
    				    isWord = false;
    			    }
    		    }
    		    p++;
    	    }
    	    if (isWord) {
    		    pos[2*n + 1] = p - 1;
    		    n++;
    	    }
    
            //this output part may be kind of  weird  due to my poor understanding of <string> , how can I modify it.
    	    string tmp(p+1, 'a');
    	    int k = 0;
    	    for (int i = n - 1; i >= 0; i--){
    		    for (int j = pos[2*i]; j <= pos[2*i + 1]; j++, k++){
    			    tmp[k] = s[j];
    		    }
    		    tmp[k++] = ' ';
    	    }
    	    if(k!=0){
        	    tmp[k-1] = '\0';
    	        string ss(k - 1, 'a');
        	    for (int j = 0; j < k - 1;j++) 	ss[j] = tmp[j];
    	        s = ss;
    	    }
    	    else s='\0';
        }
    };

Log in to reply
 

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