zoukankan      html  css  js  c++  java
  • 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 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.  "
    ]

    public class Solution {
        public List<String> fullJustify(String[] words, int L) {
          List<String> res = new ArrayList<>();
          if(words == null || words.length == 0 || L==0) {
              res.add("");
              return res;
          }
          int count = 0;
          int last = 0;
          for(int i = 0 ; i < words.length ; i++){
              if(count + words[i].length() + i - last > L){ //count 计数加该单词的长度+ 空格数 last是上一行最后一个单词
                  // fail to add new word to this line
                  int space = 0;
                  int extraSpace = 0;
                  if(i - last - 1 > 0){//因为fail,空格数要减1
                      space = (L - count) / (i - last - 1);
                      extraSpace = (L - count) % (i - last - 1);
                  } 
                  StringBuilder str = new StringBuilder();
                  for(int j = last ; j < i ; j++){   // 填单词和空格
                      str.append(words[j]);
                      if(j < i-1){  //最后一个单词后不用填空格
                          for(int k = 0 ; k < space; k++){
                              str.append(" ");
                              if(extraSpace > 0)
                                str.append(" ");
                              extraSpace --;
                          }
                      }
                  }
                  
                  for(int j = str.toString().length(); j < L; j ++){
                       str.append(" ");
                  }
                  res.add(str.toString());
                  last = i;
                  count = 0;
              }
              count += words[i].length();
          }
          
          //处理最后一行 只有一个word
          StringBuilder str = new StringBuilder();
          for(int  i = last ; i < words.length ; i++){
              str.append(words[i]);
              if(words[i].length() < L)
                str.append(" ");
          }
          for(int j = str.toString().length(); j < L; j ++){
              str.append(" ");
          }
          res.add(str.toString());
              
         return res;
        }
    }

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

  • 相关阅读:
    redis 集群目标、集群查看、配置方法及过程、哨兵配置启动
    redis 事务、Jedis事务处理流程
    redis订阅与发布(把redis作为消息中间件)
    redis 管道技术 pipeline 简介
    redis 适用场景、缓存选择、java实现
    redis 数据淘汰策略与配置
    redis 持久化策略、aof配置、测试、手动持久化、aof文件体积优化
    redis 命令行查看修改配置文件项、配置文件说明
    redis HyperLogLog 基数估算
    redis 命令select、dbsize、清空数据库、info、client
  • 原文地址:https://www.cnblogs.com/joannacode/p/6130193.html
Copyright © 2011-2022 走看看