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 considered
22                 }
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 line
29                 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                         else
37                         {
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                 } // if
49                 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 line
60                 line.clear();
61                 tmp.clear();//
62                 len = 0;
63             }//else
64         }//for
65         return res;
66     }
67 };

 

posted @ 2014-11-13 16:19  Ryan in C++  阅读(271)  评论(0编辑  收藏  举报