[leetcode] Text Justification

來源:互聯網
上載者:User

標籤:style   blog   http   java   color   strong   

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.