[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 in each line. Pad extra spaces ‘ ‘ when necessary so that each line has exactlyL 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.

click to show corner cases.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.

https://leetcode.com/problems/text-justification/



恶心题。

fullJustify([""], 2);这个Case怎么回事,fullJustify([""], 0);这也就算了,可以接受。

你明明输入空字符串,还要我给你格式化成2个空格,返回空算错,不讲道理,如果输入两个空呢,你叫我怎么格式化:fullJustify(["", ""], 2);

要我说全算是异常情况,return [];

第一轮遍历的时候用#把字符串分割出来,比如例子中的第一行就是This#is#an。

这样操作起来就很方便,如果是最后一行,直接把#替换成空格,最后补足长度。

如果非最后行,计算需要多少空格的时候就把#替换成空,用maxWidth减去length就好了。

 1 /**
 2  * @param {string[]} words
 3  * @param {number} maxWidth
 4  * @return {string[]}
 5  */
 6 var fullJustify = function(words, maxWidth) {
 7     var result = [];
 8     if(words[0] === ""){
 9         return [nBlanks(maxWidth)];
10     }
11     var row = "";
12     var curr = "";
13     for(var i = 0; i < words.length; i++){
14         if(row === ""){
15             curr = words[i];
16         }else{
17             curr = "#" + words[i];
18         }
19         if(row.length + curr.length <= maxWidth){
20             row += curr;
21         }else{
22             result.push(format(row));
23             row = words[i];
24         }
25     }
26     if(row !== ""){
27         var lastRow = row.replace(/#/g, " ");
28         result.push(lastRow + nBlanks(maxWidth - lastRow.length));
29     }
30     return result;
31
32     function format(str){
33         var res = "";
34         var splited = str.split(‘#‘);
35         if(splited.length === 1){
36             res = splited + nBlanks(maxWidth - splited[0].length);
37         }else{
38             var charsLen = str.replace(/#/g, "").length;
39             var needBlanks = maxWidth - charsLen;
40             var wordBlanks = parseInt(needBlanks / (splited.length - 1));
41             var remainBlanks = needBlanks - wordBlanks * (splited.length - 1);
42             res = splited[0];
43             for(var i = 1; i < splited.length; i++, remainBlanks--){
44                 if(remainBlanks > 0 ){
45                     res += " " + nBlanks(wordBlanks);
46                 }else{
47                     res += nBlanks(wordBlanks);
48                 }
49                 res += splited[i];
50             }
51         }
52         return res;
53     }
54     function nBlanks(n){
55         var res = "";
56         for(var i = 0; i < n; i++){
57             res += " ";
58         }
59         return res;
60     }
61 };
时间: 2024-10-15 23:21:02

[LeetCode][JavaScript]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】 Text Justification

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

[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】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

Java for LeetCode 068 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 ex

【leetcode】Text Justification(hard) ☆

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

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