首页 > 其他 > 详细

leetcode-text justification

时间:2014-11-13 18:33:36      阅读:244      评论:0      收藏:0      [点我收藏+]

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

 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 };

 

leetcode-text justification

原文:http://www.cnblogs.com/forcheryl/p/4095236.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!