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

    https://oj.leetcode.com/problems/text-justification/

    思路:字符串处理的题目,按要求依次处理各种情况即可。

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class Solution {
        public List<String> fullJustify(String[] words, int L) {
            List<String> res = new ArrayList<String>();
            if (words.length == 0) {
                return res;
            }
            if (L == 0) {//deal with L==0 and words are ""
                res = Arrays.asList(words);
                return res;
            }
    
            int len = words.length;
            int start = 0;
            int end = 0;
            int count = 0;
    
            while (start < len) {
                int i = start;
                while (i < len && count + words[i].length() <= L) {
                    count += words[i].length() + 1;
                    i++;
                }
                end = i - 1;
    
                int curWordsLen = count - (end - start + 1);
    
                if (end != len - 1)
                    adjust(res, words, L, start, end, curWordsLen, false);
                else
                    adjust(res, words, L, start, end, curWordsLen, true);
                start = end + 1;
                count = 0;
            }
    
            return res;
        }
    
        private void adjust(List<String> res, String[] words, int l, int start, int end, int wordsLen, boolean lastLine) {
    
            if (lastLine || end == start) {
                StringBuilder sb = new StringBuilder();
                for (int i = start; i <= end; i++) {
                    sb.append(words[i]);
                    if (i != end)
                        sb.append(" ");
                }
                while (sb.length() < l)
                    sb.append(" ");
    
                res.add(sb.toString());
    
            } else {
                if (start != end) {
                    int avgSpaces = (l - wordsLen) / (end - start);
                    int extraSpace = (l - wordsLen) % (end - start);
                    StringBuilder sb = new StringBuilder();
                    for (int i = start; i <= end; i++) {
                        sb.append(words[i]);
                        if (i != end) {
                            for (int j = 0; j < avgSpaces; j++)
                                sb.append(" ");
                            if (extraSpace-- > 0)
                                sb.append(" ");
                        }
                    }
                    res.add(sb.toString());
                }
    
            }
    
        }
    
        public static void main(String[] args) {
            System.out.println(new Solution().fullJustify(new String[] { "This", "is", "an", "example", "of", "text",
                    "justification." }, 16));
            System.out.println(new Solution().fullJustify(new String[] { "" }, 0));
    
        }
    }
    View Code

    第二遍记录:太烦懒得做了。。 

    参考:

    http://blog.csdn.net/linhuanmars/article/details/24063271

    http://www.cnblogs.com/TenosDoIt/p/3475275.html

  • 相关阅读:
    深入学习高级非线性回归算法 --- 树回归系列算法
    监督学习中关于线性回归问题的系统讨论
    非均衡分类问题的思考与问题与解决思路
    使用 AdaBoost 元算法提高分类器性能
    支持向量机 (SVM)分类器原理分析与基本应用
    Logistic回归分类算法原理分析与代码实现
    mysql 数据库安装步骤个人总结
    mysql可重复读现象及原理分析
    ssm所需的jar详解
    获取客户端ip地址--getRemoteAddr()和getRemoteHost() 区别
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3812744.html
Copyright © 2011-2022 走看看