首页 > 代码库 > leetcode-text justification

leetcode-text justification

这题的边边角角太多了。写的挺蛋疼的。

 1 class Solution {//by myself and AC 2 public: 3     vector<string> fullJustify(vector<string> &words, int L) { 4         vector<string> res; 5         vector<string> tmp;//store each word in a line 6         words.push_back("#");//为了方便处理队尾元素。 7         int flag = 0;//to mark the 1st word in each line 8         int len = 0;//the length of a line with one blank space between every word has been considered 9         for (int i = 0; i < words.size(); i++)10         {11             if ((len + words[i].size() <= L&&!flag || len + words[i].size() + 1 <= L)12                 && i < words.size() - 1)//len + words[i].size() + 1 <= L,注意这个+1去掉了13             {14                 if (flag == 0) {//每行的第一个单词15                     tmp.push_back(words[i]);16                     flag = 1;17                     len += words[i].size();18                 }19                 else{20                     tmp.push_back(words[i]);21                     len += words[i].size() + 1;//blank space between words has been considered22                 }23             }24             else{//已经不能再加新单词了,开始清算,统计到line中。(有可能i=words.size()-1)25                 if (i<words.size()-1 )i--;//注意这里的i--!因为当上一步判断中发现已经过长了的时候就把当前的单词给轮空了。26                 string line = "";27                 int diff_sum = L - len;28                 if (i == words.size() - 1) diff_sum = 0;//tackle the last line29                 if (tmp.size() > 1)30                 {31                     int gapBasis = diff_sum / (tmp.size() - 1);32                     int remainder = diff_sum % (tmp.size() - 1);33                     for (int j = 0; j < tmp.size(); j++)34                     {35                         if (j == 0) line += tmp[j];36                         else37                         {38                             line.insert(line.end(), 1 + gapBasis + (remainder>0),  );39                             line += tmp[j];40                             remainder--;41                         }42                     }43                     if (i == words.size() - 1)//对于最后一行而言,要补全剩下的空格,凑够一行。44                     {45                         line.insert(line.end(),L-line.size(), );46                     }47                     res.push_back(line);48                 } // if49                 else if (tmp.size() == 1)50                 {51                     line += tmp[0];52                     line.insert(line.end(), diff_sum,  );53                     if (i == words.size() - 1)//对于最后一行而言,要补全剩下的空格,凑够一行。54                     {55                         line.insert(line.end(), L - line.size(),  );56                     }57                     res.push_back(line);58                 }59                 flag = 0;//prepare for the next line60                 line.clear();61                 tmp.clear();//62                 len = 0;63             }//else64         }//for65         return res;66     }67 };

 

leetcode-text justification