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

 

转载于:https://www.cnblogs.com/forcheryl/p/4095236.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值