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 exactlyL 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.

    click to show corner cases.

    public class Solution {
        public ArrayList<String> fullJustify(String[] words, int L) {
          ArrayList<String> result = new ArrayList<String>();
            StringBuffer stb = new StringBuffer();
            if(L > 0){
                for(String s : words){
                    if(s.length() + stb.length() > L){
                        String st = justification(stb, L);
                        result.add(st);
                        stb = new StringBuffer();
                    }
                    stb.append(s+" ");            
                }
    
                if(stb.length() != 0){
                    if(stb.charAt(stb.length() - 1) == ' ')
                        stb.deleteCharAt(stb.length() - 1);
                    if(L - stb.length() > 0)
                        stb.append(blankCharArray(L - stb.length()));
                    result.add(stb.toString());
                }
            }
            else
                result.add("");
            return result;    
        }
    
        public String justification(StringBuffer stb, int length){
            //String result = null;
            int len = stb.length();
            if(stb.charAt(len - 1) == ' ')
                stb.deleteCharAt(len - 1);
            int blanks = 0;    
            for(int i = 0; i < stb.length(); ++i){
                if(stb.charAt(i) == ' ')
                    ++blanks;        
            }
            int len2 = stb.length();
            int insertBlanks = length - len2;
            if(blanks > 0 && insertBlanks > 0){
                for(int j = len2 -1 ; j > 0; --j){
                    if(stb.charAt(j - 1) != ' ' && stb.charAt(j) == ' '){
                        stb.insert(j, blankCharArray(insertBlanks / blanks));
                        insertBlanks -= insertBlanks / blanks;
                        --blanks;
                    }        
                }    
            }
            else if(blanks == 0 && insertBlanks > 0)
                stb.append(blankCharArray(insertBlanks));
            return stb.toString();
        }
    
        public char[] blankCharArray(int length){
            char[] inserted = new char[length];
            for(int i = 0; i < length; ++i){
                inserted[i] = ' ';
            }
            return inserted;    
        }
    }
    

     

  • 相关阅读:
    61. 最长不含重复字符的子字符串
    60. 礼物的最大价值 (未理解)
    59. 把数字翻译成字符串
    58. 把数组排成最小的数
    57. 数字序列中某一位的数字 (不懂)
    spring data jpa 官方文档
    idea 编译报错 源发行版 1.8 需要目标发行版 1.8
    idea maven 依赖报错 invalid classes root
    solr
    spring boot 官方文档
  • 原文地址:https://www.cnblogs.com/averillzheng/p/3612798.html
Copyright © 2011-2022 走看看