zoukankan      html  css  js  c++  java
  • LeetCode

    题目:

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

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

    思路:线性往前推

    package manipulation;
    
    import java.util.ArrayList;
    import java.util.List;
    
    public class TextJustification {
    
        public List<String> fullJustify(String[] words, int maxWidth) {
            List<String> res = new ArrayList<String>();
            int n = words.length;
            int i = 0;
            int len = 0;
            int start = 0;
            for (; i < n; ++i) {     
                if (len + words[i].length() + (i == start ? 0 : 1) > maxWidth) {
                    justify(words, start, i - 1, res, maxWidth);
                    start = i;
                    len = 0;
                }
                len += words[i].length() + (i == start ? 0 : 1);
            }
            justify(words, start, n - 1, res, maxWidth);
            return res;
        }
        
        private void justify(String[] words, int start, int end, List<String> res, int L) {
            int count = 0;
            int total = 0;
            for (int i = start; i <= end; ++i) {
                ++count;
                total += words[i].length();
            }
            int rem = L - total;
            // Only one word in this line
            if (count == 1) {
                String space = "";
                for (int i = 0; i < rem; ++i) space += " ";
                res.add(words[start] + space);
            } else if (end == words.length - 1) { // This is the last line
                String s = words[start];
                for (int i = 1; i <= count - 1; ++i) {
                    s += " " + words[start + i];
                }
                for (int i = 0; i < rem - count + 1; ++i) s+= " ";
                res.add(s);
            } else { // General case
                int avg = rem / (count - 1);
                int leftMore = rem - avg * (count  - 1);
                String s = words[start];
                for (int i = 1; i <= count - 1; ++i) {
                    for (int j = 0; j < avg; ++j) s += " ";
                    s += leftMore-- > 0 ? " " : "";
                    s += words[start + i];
                }
                res.add(s);
            }
        }
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            String[] words = { "This", "is", "an", "example", "of", "text", "justification." };
            TextJustification t = new TextJustification();
            for (String s : t.fullJustify(words, 16)){
                System.out.println(s);
            }
        }
    
    }
  • 相关阅读:
    SQL里的EXISTS与in、not exists与not in
    N秒后自动跳转
    Array类型的扩展
    css中block与inline的区别
    数据绑定表达式语法(Eval,Bind区别)
    case
    SCOPE_IDENTITY、IDENT_CURRENT 和 @@IDENTITY的比较 (转载)
    内容随鼠标漂移
    IIS下下伪静态html(URL Rewrite)设置方法
    sql查询含有某列名的所有表
  • 原文地址:https://www.cnblogs.com/null00/p/5087730.html
Copyright © 2011-2022 走看看