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

    Ref: http://fisherlei.blogspot.com/2013/01/leetcode-text-justification.html
     
     
    public class Solution {
        public ArrayList<String> fullJustify(String[] words, int L) {
            int wordsCount = words.length;
            ArrayList<String> result = new ArrayList<String>();
            int curLen = 0;
            int lastI = 0;
            for (int i = 0; i <= wordsCount; i++) {
               // 如果是最后一个单词 或 现有长度加上下一个单词和每个单词至少空一个格的长度和大于 L, 就不加下一个单词了,将现有单词组合成string
                if (i == wordsCount || curLen + words[i].length() + i - lastI > L) {
                    StringBuffer buf = new StringBuffer();
                      // 计算需要多少个空格
                    int spaceCount = L - curLen;
                    //计算要把空格分成几份,也就是这一行的单词个数-1
                    int spaceSlots = i - lastI - 1;
                    if (spaceSlots == 0 || i == wordsCount) {
                        for(int j = lastI; j < i; j++){
                            buf.append(words[j]);
                            if(j != i - 1)
                                appendSpace(buf, 1);
                        }
                        appendSpace(buf, L - buf.length());
                    } else {
                        // 两个单词之间要填多少个空格
                        int spaceEach = spaceCount / spaceSlots;
                        // 剩余的空格的数量
                        int spaceExtra = spaceCount % spaceSlots;
                        for (int j = lastI; j < i; j++) {
                            buf.append(words[j]);
                            // j 不是这一行的最后一个数
                            if (j != i - 1) 
                            //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
                                appendSpace(buf, spaceEach + (j - lastI < spaceExtra ? 1 : 0));
                        }
                    }
                    result.add(buf.toString());
                    lastI = i; // 上一行遍历到的那个单词的下标(注意此单词还未添加到result中)
                    curLen = 0;
                }
                if (i < wordsCount)
                    curLen += words[i].length();
            }
            return result;
        }
    
        private void appendSpace(StringBuffer sb, int count) {
            for (int i = 0; i < count; i++)
                sb.append(' ');
        }
    }
  • 相关阅读:
    【转】在Ubuntu上下载、编译和安装Android最新源代码
    【转】Linux(ubuntu14.04)上编译Android4.4源码的环境搭建及编译全过程
    linux kernel API and google android compile guide
    【转】如何单独编译Android源代码中的模块--不错
    【转】Android源代码编译命令m/mm/mmm/make分析--不错
    【转】linux设备驱动之MMC SD卡——核心层简单分析
    DELL笔记本拆机添加内存条
    【转】windows7 64位系统认不出8g内存显示只有3G可用
    matlab 神经网络工具箱的实用
    学习算法收敛条件的判断
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3544621.html
Copyright © 2011-2022 走看看