【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. 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."]
L16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

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


 

题解:题目倒是不难,按照题目一步步走就是了,就是细节太多了,交了好多次T_T

从位置i开始往前取词,直到取到的词长度大于L停止取词,假设此时取到第j个词,用curLen统计这些词和它们之间的空格的长度,那么剩下来要额外平均分配到各个单词之间的空格数目就是totalSpaces = L-curLen,所以每个单词之间平均分配(L-curLen)/(j-i)个空格,但是有可能L-curLen不能被(j-i)除尽,那么余出来的空格又要一个一个插入到第一个单词间隙,第二个单词间隙......(注意不是全部插入到第一个单词间隙中)。对于最后一行,将totalSpaces直接设置为0,即不添加额外的空格,然后在结尾处填充空格使其长度达到L。

代码如下:

复制代码
 1 public class Solution {
 2     private String genericSpaces(int num){
 3         StringBuffer sb = new StringBuffer();
 4         for(int i = 0;i < num;i++)
 5             sb.append(" ");
 6         return sb.toString();
 7     }
 8     public List<String> fullJustify(String[] words, int L) {
 9         List<String> answer = new ArrayList<String>();
10         int curLen = 0;
11         for(int i = 0;i < words.length;){
12             curLen = 0;
13             int j = i;
14             for(;j<words.length && curLen+words[j].length()<=L;){
15                 curLen = curLen + words[j].length() + 1;
16                 j++;
17             }
18             curLen--;
19             int totalSpace = L-curLen;
20             j--;
21             int eachSpace = j==i?0:totalSpace/(j-i);
22             //handle last line, there are only one spaces bewteen words
23             if(j == words.length-1){
24                 totalSpace = 0;
25                 eachSpace = 0;
26             }
27             StringBuffer concate = new StringBuffer();
28             for(int k = i;k<=j;k++){
29                 concate.append(words[k]);
30                 if(k != j)
31                     concate.append(" ");
32                 String spaces;
33                 //if totalSpace can't be evenly distributed
34                 if(totalSpace-eachSpace*(j-i)!=0){
35                     spaces = genericSpaces(eachSpace+1);
36                     totalSpace--;
37                 }
38                 else {
39                     spaces = genericSpaces(eachSpace);
40                 }
41                 //last word isn't followed by spaces
42                 if(k != j)
43                     concate.append(spaces);
44             }
45             //to handle the last line, adding extra spaces to reach length L
46             while(concate.length()<L)
47                 concate.append(" ");
48             answer.add(concate.toString());
49             i=j+1;
50         }
51         return answer;
52     }
53 }
复制代码

 

posted @   SunshineAtNoon  阅读(200)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示