forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
41 lines (41 loc) · 1.28 KB
/
Solution.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
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int n = words.size();
vector<string> result;
for (int i = 0; i < n; i++)
{
int begin = i;
int wordLen = words[i].size();
while (i + 1 < n && words[i + 1].size() + wordLen + 1 <= maxWidth)
{
wordLen += words[++i].size() + 1;
}
int numberofWords = i - begin + 1;
int space = 1;
int extraSpace = 0;
if (numberofWords > 1 && i < n - 1)
{
int remaining = maxWidth - wordLen;
space = remaining / (numberofWords - 1) + 1;
extraSpace = remaining % (numberofWords - 1);
}
string line = words[begin];
for (int j = 1; j < numberofWords; j++)
{
line.append(space, ' ');
if (j <= extraSpace)
{
line.push_back(' ');
}
line += words[begin + j];
}
if (line.size() < maxWidth)
{
line.append(maxWidth - line.size(), ' ');
}
result.emplace_back(line);
}
return result;
}
};