Please help me run the code! I run it in my computer correctly but OJ does not.


  • 0
    D
    class Solution {
    public:
        vector<string> fullJustify(vector<string> &words, int L) {
            vector<string> res;
            int index=0;
            
            int emptyWordCnt=0;//special case!used when all the words is empty
            while(index<words.size())
            {
                vector<string> line;
                int basicLen=0;
                while(index<words.size())
                {
                    if(words[index].size()==0)
                    {
                        index++;
                        emptyWordCnt++;
                        continue;
                    }
                    //imagine that before the first word there exist a blank
                    if((basicLen+words[index].size()+1)<=(L+1))
                    {
                        line.push_back(words[index]);
                        basicLen=basicLen+words[index].size()+1;
                    }
                    else
                    {
                        break;
                    }
                    index++;
                }
                //remove the imagined leading blank
                basicLen--;
                if(basicLen>0)
                {
                    string strLine;
                    int padding=L-basicLen;
                    int pcnt=line.size()-1;
                    if(pcnt==0)
                    {
                        //only one word
                        strLine.append(line[0]);
                        strLine.append(padding,' ');
                    }
                    else
                    {
                        int basicpadding=padding/pcnt;
                        int firstpadding=padding-basicpadding*pcnt;
                        //word is separated by at least one blank
                        basicpadding++;
                        strLine.append(line[0]);
                        strLine.append(basicpadding+firstpadding,' ');
                        for(int i=1;i<line.size();++i)
                        {
                            strLine.append(line[i]);
                            if(i!=(line.size()-1))
                            {
                                strLine.append(basicpadding,' ');    
                            }
                            
                        }
                    }
                    res.push_back(strLine);
                }
            }
            //handle the referred special case
            if(emptyWordCnt==words.size())
            {
                if(L==0)
                {
                    res.assign(emptyWordCnt,string());
                }
                else
                {
                    res.push_back(string(L,' '));
                }
            }
            return res;
        }
    };
    

    This is the test code

    int main()
    {
    	Solution s;
    	string ws[]={"What","must","be","shall","be."};
    	vector<string> words(ws,ws+5);
    	int L=12;
    	vector<string> res=s.fullJustify(words,L);
    	for(int i=0;i<res.size();++i)
    	{
    		cout<<res[i]<<endl;
    	}
    	return 0;
    }

  • 0
    D

    Sorry, I find out what is wrong. I misunderstand the question,which has claimed that the last line need to be treated specially.


Log in to reply
 

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