LeetCode Online Judge 题目C# 练习 - 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.

 1         public static List<string> TextJustification(List<string> words, int L)
 2         {
 3             List<string> ret = new List<string>();
 4             if (L == 0)
 5             {
 6                 ret.Add("");
 7                 return ret;
 8             }
 9             int reminder = 0;
10             int spaces = 0;
11             string toinsert = "";
12 
13             int tempLength = 0;
14             int count =0;
15             for (int i = 0; i < words.Count; i++)
16             {
17                 tempLength += words[i].Length;
18                 count++;
19                 // length of words + spaces >= L
20                 if (tempLength + count - 1 >= L)
21                 {
22                     // if bigger than L, remove the last word
23                     if (tempLength + count - 1 > L)
24                     {
25                         tempLength -= words[i].Length;
26                         i--;
27                         count--;
28                     }
29 
30                     if (count > 1)
31                         reminder = (L - tempLength) % (count - 1);
32 
33                     spaces = 0;
34                     toinsert = "";
35                     for (int j = 1; j <= count; j++)
36                     {
37                         if (count > 1)
38                             spaces = (L - tempLength) / (count - 1);
39                         else
40                             spaces = L - tempLength;
41                         if (reminder > 0)
42                         {
43                             spaces += 1;
44                             reminder -= 1;
45                         }
46                         toinsert += words[i - count + j];
47                         if(j < count)
48                             toinsert += new string(' ', spaces);
49                         if (j == count)
50                         {
51                             toinsert += new string(' ', L - toinsert.Length);
52                         }
53                     }
54 
55                     ret.Add(toinsert);
56 
57                     count = 0;
58                     toinsert = "";
59                     tempLength = 0;
60                     continue;
61                 }
62                 // last line
63                 if (i == words.Count - 1)
64                 {
65                     for (int j = i - count + 1; j <= i; j++)
66                     {
67                         toinsert += words[j];
68                         if (j < i)
69                             toinsert += " ";
70                         if (j == i)
71                         {
72                             toinsert += new string(' ', L - toinsert.Length);
73                         }
74                     }
75                     ret.Add(toinsert);
76                 }
77             }
78 
79             return ret;
80         }

代码分析:

  没有什么算法可言,小心出错就可以了。请注意,这个代码可是可以过large test case 的喔

posted @ 2012-10-22 21:30  ETCOW  阅读(314)  评论(0编辑  收藏  举报