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

  • 相关阅读:
    VS生成Map文件
    Google Android Studio Kotlin 开发环境配置
    byte数组存储到mysql
    C# 读取 appconfig文件配置数据库连接字符串,和配置文件
    关于byte[]与string、Image转换
    运行vs2010,Debug时发生“无法启动程序"http://localhost:xxx",系统找不到指定文件
    【转】使用Navicat for Oracle新建表空间、用户及权限赋予
    eclipse创建maven项目
    使用eclipse自动生成WSDL客户端代码
    使用Apache CXF根据wsdl文件生成代码
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3812744.html
Copyright © 2011-2022 走看看