I cannot tell what happened when the test "What","must","be","shall","be." used,the extra space in the end but I do not put any space


  • 0
    X

    class Solution {
    public:

    •  vector<string> fullJustify(vector<string>& words, int maxWidth) {
        if (words.empty() || maxWidth <= 0)return words;
      int first = 0;
      vector<string> wordline;
      for (first = 0; first<words.size();)
      {
      	int count = words[first].size();
      	int last = first;
      	while (last<words.size() - 1 && count + words[last + 1].size() + 1 <= maxWidth)
      	{
      		last++;
      		count += words[last].size()+1;
      	}
      	if (last == first)
      	    {
      	        
      	        wordline.push_back(words[first]);
      	        wordline.back().append(string(maxWidth - words[first].size(), ' '));
      	        first++;
      	    }
      	else {
      		int numspace = (maxWidth - count) / (last - first)+1;
      		int res = (maxWidth - count) % (last - first);
      		string line = words[first++];
      		while(res--){
      		    line.append(string(numspace + 1, ' '));
      		    line.append(words[first++]);
      		} ;
      		while(first<=last){
      		    line.append(string(numspace, ' ') );
      		    line.append(words[first]);
      		    first++;
      		}
      		wordline.push_back(line);
      	}
      	
      }
      return wordline;
      
      }
      };

  • 0
    X

    On my own computer,the answer just like what it expected, but when I submit it, there are three spaces in the end


Log in to reply
 

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