码迷,mamicode.com
首页 > 其他好文 > 详细

Text Justification

时间:2016-08-06 06:56:53      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

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.

分析:

把字符串一批一批的处理即可。注意,最后一组的处理方式不一样。

 1 public class Solution {
 2     public List<String> fullJustify(String[] words, int maxWidth) {
 3         List<String> listAll = new ArrayList<String>();
 4         List<String> list = new ArrayList<String>();
 5         int count = 0;
 6         for (int i = 0; i < words.length; i++) {
 7             list.add(words[i]);
 8             count += words[i].length();
 9             if (i == words.length - 1) {
10                 listAll.add(helper(list, maxWidth, true));
11             } else if (count + words[i + 1].length() + 1 > maxWidth) {
12                 listAll.add(helper(list, maxWidth, false));
13                 list.clear();
14                 count = 0;
15             } else {
16                 count++; // space
17             }
18         }
19         return listAll;
20     }
21 
22     private String helper(List<String> list, int maxWidth, boolean lastBatch) {
23 
24         StringBuilder sb = new StringBuilder();
25         if (lastBatch || list.size() == 1) {
26             int count = 0;
27             for (int i = 0; i < list.size(); i++) {
28                 sb.append(list.get(i));
29                 count += list.get(i).length();
30                 if (i != list.size() - 1) {
31                     sb.append(" ");
32                     count++;
33                 }
34             }
35             sb.append(filler(maxWidth - count));
36             return sb.toString();
37         }
38         int letterCount = 0;
39         for (String str : list) {
40             letterCount += str.length();
41         }
42 
43         int spaceRemaining = maxWidth - letterCount;
44         int numWhiteSpaces = spaceRemaining / (list.size() - 1);
45 
46         String filler1 = filler(numWhiteSpaces);
47         String filler2 = filler1 + " ";
48 
49         int leftOver = spaceRemaining - numWhiteSpaces * (list.size() - 1);
50 
51         for (int i = 0; i < list.size(); i++) {
52             sb.append(list.get(i));
53             if (i != list.size() - 1) {
54                 if (leftOver > 0) {
55                     sb.append(filler2);
56                 } else {
57                     sb.append(filler1);
58                 }
59             }
60             leftOver--;
61         }
62         return sb.toString();
63     }
64     
65     private String filler(int size) {
66         String filler = "";
67         for (int i = 1; i <= size; i++) {
68             filler += " ";
69         }
70         return filler;
71     }
72 }

 

Text Justification

标签:

原文地址:http://www.cnblogs.com/beiyeqingteng/p/5743197.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!