zoukankan      html  css  js  c++  java
  • LeetCode(68) Text Justification

    题目

    Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.

    You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ’ ’ when necessary so that each line has exactly L characters.

    Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.

    For the last line of text, it should be left justified and no extra space is inserted between words.

    For example,
    words: [“This”, “is”, “an”, “example”, “of”, “text”, “justification.”]
    L: 16.

    Return the formatted lines as:
    [
    “This is an”,
    “example of text”,
    “justification. ”
    ]

    分析

    给定一个字符串数组以及规定长度,按规则将其分组输出;

    题目本身是不难的,主要是规则繁杂:

    1. 首先,输出以是否为末行分为两类;
    2. 对于非末行单词组,又以其包含的单词个数分为两类,一是单个单词,二是多个单词;

    第一步,讨论非末行单词组合:
    (1)若该组只包含一个单词,规定其左对齐,不足指定长度以空格填充;
    (2)若该组包含count个单词,那么它有(count-1)个间隔,每个间隔放置一个空格;此时,求出不足指定长度需要的额外空格数目,extraSpace,每个单词间隔填充extra/(count-1)个空格;此时,若不整除那么前extra%(count-1)个间隔再次填充一个空格;

    第二步,讨论末行单词组合:
    (1)若只有一个单词,左对齐,不足指定长度以空格填充;
    (2)若该组有count个单词,那么它有(count-1)个间隔,每个间隔放置一个空格;不足指定长度,末尾填充;

    AC代码

    class Solution {
    public:
        vector<string> fullJustify(vector<string>& words, int maxWidth) {
            if (words.empty())
                return vector<string>();
            vector<string> ret;
    
            int sz = words.size();
    
            /*sumLen记录当前字符串长度,count记录包含的单词个数*/
            vector<string> tmp;
            int sumLen = 0, count = 0;
            for (int i = 0; i < sz; ++i)
            {
                /*判断是否可以添加一个字符串*/
                if ((sumLen + words[i].length() + count) <= maxWidth)
                {
                    /*满足要求,单词个数增一,保存*/
                    ++count;
                    sumLen = sumLen + words[i].length();
                    tmp.push_back(words[i]);
                    continue;
                }//if
                else{
                    /*只有一个单词,左对齐*/
                    if (1 == count)
                    {
                        string str = tmp[0];
                        while (str.length() < maxWidth)
                            str += " ";
                        ret.push_back(str);
                    }//if
                    else{
                        string str = "";
                        /*计算多余的空格总数,每个间隔至少一个空格*/
                        int extraSpace = maxWidth - sumLen - count + 1;
                        /*每个间隔需再增加的间隔*/
                        int everySpace = extraSpace / (count - 1);
                        /*前间隔需要额外放置一个空格的间隔数*/
                        int frontSpace = extraSpace % (count - 1);                  
    
                        for (int k = 0; k < count - 1; ++k)
                        {
                            int j = 0;
                            while (j < everySpace + 1)
                            {
                                tmp[k] += " ";
                                ++j;
                            }//while
                        }//for
                        /*前frontSpace个间隔需要再放一个空格*/
                        for (int k = 0; k < frontSpace; ++k)
                        {
                            tmp[k] += " ";
                        }
                        /*连接这些字符串*/
                        for (int k = 0; k < count; ++k)
                        {
                            str += tmp[k];
                        }//for
                        ret.push_back(str);             
                    }//else
                }//else
                tmp.clear();
                count = 0;
                sumLen = 0;
                --i;
            }//for
            /*处理最后一组,也就是尾行*/
            /*只有一个单词,左对齐*/
            if (1 == count)
            {
                string str = tmp[0];
                while (str.length() < maxWidth)
                    str += " ";
                ret.push_back(str);
            }//if
    
            if(count > 1){
                string str = "";                
                /*末行的每个单词间放一个空格,其余空格放在尾部*/
                for (int k = 0; k < count - 1; ++k)
                {
                    str = str + tmp[k] + " ";
                }//for
                str += tmp[count - 1];
                while (str.length() < maxWidth)
                    str += " ";
                ret.push_back(str);
            }//else     
            return ret;
        }
    };

    GitHub测试程序源码

  • 相关阅读:
    CDM业务单据,表体单价列赋值所需要注意的问题
    用友CDM系统,将货位间商品移库单(一步)修改为内调出入库单(一步)方法使用
    用友CDM系统“货位间商品移库单(一步)”表体增加“货位可用数量”字段,根据表头的选择的货位自动带出数值
    用友CDM系统期初导入商品资料经验
    SSIS典型应用场景分析
    sql2000执行sql2005导出的数据脚本时出现“提示含有超过64K限度的行”(转)
    将两个不同格式的XML文件,进行节点对照,并生成一个用于对照功能的XML
    winform 根据NAME查找控件
    Nginx服务配置编写
    Nginx安装部署实例
  • 原文地址:https://www.cnblogs.com/shine-yr/p/5214714.html
Copyright © 2011-2022 走看看