68 Text Justification

解题思路

  • 将单词分组打出,每组长度为L;
  • 除了最后一行,单词需要左右对齐,平铺效果,中间以空格均匀填充;
  • 最后一行,总长度也是L,但是要求左对齐,以一个空格隔开即可;

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class Solution
{
public:
vector<string> fullJustify(vector<string> &words, int maxWidth)
{
int len = words.size();
vector<string> res;
vector<string> temp;
string str;
int count = 0;
int totalLen = 0;
for(int i = 0; i < len; ++i)
{
if(count + totalLen + words[i].size() > maxWidth)
{
str = "";
str += temp[0];
if(count == 1)
{
str += string(maxWidth - totalLen, ' ');
}
else if(count == 2)
{
str += string(maxWidth - totalLen, ' ');
str += temp[1];
}
else
{
int block = (maxWidth - totalLen) / (count - 1);
int last = maxWidth - totalLen - block * (count - 1);
for(int j = 1; j < count; ++j)
{
str += string(block, ' ');
if(last-- > 0)
str += ' ';
str += temp[j];
}
}
res.push_back(str);
temp.clear();
temp.push_back(words[i]);
count = 1;
totalLen = words[i].size();
}
else
{
temp.push_back(words[i]);
count++;
totalLen += words[i].size();
}
}
if(count)
{
str = "";
str += temp[0];
for(int j = 1; j < temp.size(); ++j)
{
str += ' ';
str += temp[j];
}
str += string(maxWidth - totalLen - count + 1, ' ');
res.push_back(str);
}
return res;
}
};
-------------本文结束感谢您的阅读-------------
0%