zoukankan      html  css  js  c++  java
  • 【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 }
  • 相关阅读:
    saltstack总结-2018-0620
    我的书籍《Redis 源码日志》
    深入剖析 redis 主从复制
    深入剖析 redis AOF 持久化策略
    初探单点登录 SSO
    深入剖析 redis RDB 持久化策略
    深入剖析 redis 事件驱动
    memcached 源码阅读笔记
    Django 源码小剖: Django ORM 查询管理器
    Django 源码小剖: Django 对象关系映射(ORM)
  • 原文地址:https://www.cnblogs.com/sunshineatnoon/p/3870500.html
Copyright © 2011-2022 走看看