• [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(' ');
        }
    }
  • 相关阅读:
    Eclipse导入Spring Boot项目后pom.xml出现红叉的解决办法
    ubuntu18.04中将刚下载解压的eclipse添加到启动器
    easyui datagrid设置一开始不加载数据
    Spring Boot开发八字箴言(以我过去这段时间的经验总结得到)
    Spring Boot中mybatis insert 如何获得自增id
    jquery控制一个元素是否显示
    easyui-datagrid配置宽度高度自适应
    html页面js响应回车
    Node.js ORM框架Sequelize使用示例
    Java遍历日期代码
  • 原文地址:https://www.cnblogs.com/RazerLu/p/3544621.html
Copyright © 2020-2023  润新知