[LeetCode][JavaScript]Text Justification

來源:互聯網
上載者:User

標籤:

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);這也就算了,可以接受。

你明明輸入Null 字元串,還要我給你格式化成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 };

 

 

 

 

 

 

 

[LeetCode][JavaScript]Text Justification

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.