【一天一道LeetCode】#68. Text Justification

一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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 Lcharacters.

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. ”

]

Note: Each word is guaranteed not to exceed L in length.

(二)解题

题目大意:按照一定的格式对文本进行对齐。

需要注意以下几点:

1、只有一个单词直接在后面补空格,如“a”,5 输出“a ”

2、最后一组单词不需要对齐,如“a”,“b” 5 输出“a b ”

3、单词与单词之间至少要有一个空格隔开

具体 思路见代码注释:

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> ret;//返回值
        int width = words[0].size();
        int num = 1;
        int last = 0 ;//纪录每一次maxWidth的起始序号
        for(int i = 1 ; i<words.size() ; i++)
        {
            width+=words[i].size();
            num++;
            if(width+num-1>maxWidth)//这里要注意每个单词之间要用空格隔开
            {
                width-=words[i].size();//清除掉最后一个数
                num--;
                string temp;
                if(num==1){//只有一个单词的情况
                    temp+=words[i-1];
                    while(temp.size()!=maxWidth) temp+=" ";//在后面补齐空格
                    ret.push_back(temp);
                }
                else//多个单词,但不是结尾的情况
                {
                    int blankWidth = maxWidth - width;
                    int gap = 0;
                    for(int j = last ; j < i ; j++)
                    {
                        if(j==i-1) gap=0;//最后一个单词后面不加空格
                        else {
                            gap = blankWidth/(num-1);//每一次进来都要算需要增加多少空格
                            gap = blankWidth%(num-1)>0?gap+1:gap;//保证均匀分布
                            blankWidth -=gap;
                        }
                        temp+=words[j];
                        while(gap>0&&gap--) temp+=" ";
                        num--;
                    }
                    ret.push_back(temp);
                }
                //初始化下一个循环
                last=i;
                width = words[i].size();
                num=1;
            }
        }
        if(last<words.size()){//考虑末尾不足maxWidth的情况
            int j = last;
            string temp;
            while(j<words.size()){//先按一个空格添加words
                temp+=words[j++];
                if(temp.size()<maxWidth) temp+=" ";
            }
            while(temp.size()<maxWidth) temp+=" ";//最后不足maxWidth就用空格补足
            ret.push_back(temp);
        }
        return ret;
    }
};
时间: 2024-10-31 12:41:39

【一天一道LeetCode】#68. Text Justification的相关文章

LeetCode --- 68. Text Justification

题目链接: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

[leetcode] 68. Text Justification 解题报告

题目链接: https://leetcode.com/problems/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; tha

leetcode 68 Text Justification ----- java

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 ex

Leetcode 68. Text Justification 文本调整 解题报告

1 解题思想 这道题,其实我也想不通为什么要标记为Hard模式,题目的大意就是对一个字符串数组进行格式化调整,输出对应的句子. 要求有: 1.每一行的字符串长度不能超过一个固定长度maxWidth 2.每两个单词之间必须有一个空格,如果一行之间的单词之间空格不能细分,那么必须左边的空格多,右边的少.并且,空格多的地方只比右边少的多一个 3.最后一行不适用2的空格方式,正常的每个单词空一格就好,最后留白就好 提前贴个解释: * 这道题关键在于仔细的处理每一个步骤: * 1.每一行选择K的单词,K个

[leetcode]68. Text Justification文字对齐

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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

No.68 Text Justification

No.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

【leetcode】 Text Justification

问题: 给定一个字符串数组words,一个整数L,将words中的字符串按行编辑,L表示每行的长度. 要求: 1)每个单词之间至少是有一个空格隔开的. 2)最后一行每个单词间只间隔一个空格, 最后一个单词后不足L长度的用空格填充. 3)除最后一行外,其他行进行填充长度的空格要均分,不能均分的,将余数代表的空格数依次填充在行左. For example, words: ["This", "is", "an", "example"

[LeetCode][JavaScript]Text Justification

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 i

【leetcode】Text Justification

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 i