-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathp068.cpp
75 lines (75 loc) · 2.22 KB
/
p068.cpp
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
67
68
69
70
71
72
73
74
75
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int left = 0, right, i, j, k;
int lencnt, wordcnt, tmplen;
int size = words.size();
vector<string> result;
string addstr(maxWidth, ' ');
while (left < size)
{
for (i = 0; i < maxWidth; i++)
{
addstr[i] = ' ';
}
lencnt = wordcnt = 0;
for (right = left; right < size; right++)
{
tmplen = words[right].length();
if (lencnt+tmplen+wordcnt <= maxWidth)
{
lencnt += tmplen;
wordcnt++;
}
else
{
break;
}
}
if (wordcnt == 1)
{
for (i = 0; i < words[left].length(); i++)
{
addstr[i] = words[left][i];
}
}
else if (right == size)
{
k = 0;
for (i = left; i < right; i++)
{
for (j = 0; j < words[i].length(); j++)
{
addstr[k++] = words[i][j];
}
k++;
}
}
else
{
int rightslot = (maxWidth-lencnt)/(wordcnt-1);
int leftslot = rightslot+1;
int leftslotcnt = maxWidth-lencnt-rightslot*(wordcnt-1);
k = 0;
for (i = left; i < right; i++)
{
for (j = 0; j < words[i].length(); j++)
{
addstr[k++] = words[i][j];
}
if (i-left < leftslotcnt)
{
k += leftslot;
}
else
{
k += rightslot;
}
}
}
result.push_back(addstr);
left = right;
}
return result;
}
};