[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; 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.  "
]

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

思路:就是一个字符串处理的题目,并不难,但是却很难写的比较优雅.

两个代码如下,第一个是我自己的,代码比较长,0ms.第二个是参考的别人的,4ms,但是比较漂亮.

第二种方法很巧妙的求不均衡空格,就是利用位置与除余的关系来让左边分配多余空格.

代码如下:

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        int cnt = 0, left = 0;
        vector<string> result;
        for(int i =0; i< words.size(); i++)
        {
            cnt += words[i].size();
            if(cnt+i-left > maxWidth || i+1==words.size())
            {
                if(cnt+i-left > maxWidth) cnt -= words[i--].size();
                string str = words[left];
                for(int j = left+1; j<= i; j++)
                {
                    int m = maxWidth-cnt, n = i-left;
                    if(i+1==words.size()) str += " ";
                    else str.append(m/n + (j-left-1<m % n), ' ');
                    str += words[j];
                }
                str.append(maxWidth-str.size(), ' ');
                result.push_back(str);
                left = i+1, cnt = 0;
            }
        }
        return result;
    }
};
class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> result;
        for(int i=0, m, n; i< words.size(); i+=m)
        {
            for(m=0,n=0;i+m<words.size()&&m+n+words[i+m].size()<=maxWidth; m++)
                n+= words[i+m].size();
            string str = words[i];
            for(int j = 0; j < m-1; j++)
            {
                if(i+m >= words.size()) str+= " ";
                else
                    str.append((maxWidth-n)/(m-1) + (j<(maxWidth-n)%(m-1)), ' ');
                str += words[i+j+1];
            }
            str.append(maxWidth-str.size(), ' ');
            result.push_back(str);
        }
        return result;
    }
};

第二种参考: https://leetcode.com/discuss/13610/share-my-concise-c-solution-less-than-20-lines

时间: 2024-10-09 19:45:32

[leetcode] 68. Text Justification 解题报告的相关文章

【LeetCode】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. P

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 文本调整 解题报告

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

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

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文字对齐

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: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

【leetcode】 Text Justification

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