zoukankan      html  css  js  c++  java
  • 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 };
  • 相关阅读:
    PHP解决imagecopymerge合成图片后黑色边角的问题
    cmake简单介绍
    python pytest测试框架介绍七 ---测试用例显示用例注释名称
    docker day001
    C语言 字节数组和hex和互相转换
    glsl 内置数学函数
    springboot分析——自定义启动类
    springboot源码分析——自动配置流程
    springboot源码分析——SPI扩展机制
    springboot分析——与其他组件的整合(JPA规范/atomikos/redis)
  • 原文地址:https://www.cnblogs.com/forcheryl/p/4095236.html
Copyright © 2011-2022 走看看