-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path68.cpp
More file actions
executable file
·40 lines (40 loc) · 1.32 KB
/
68.cpp
File metadata and controls
executable file
·40 lines (40 loc) · 1.32 KB
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
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
vector<string> res;
int i = 0;
while (i < words.size()) {
int j = i, len = 0;
while (j < words.size() && len + words[j].size() + j - i <= L) {
len += words[j++].size();
}// let j-i = space number
string out;
int space = L - len;
for (int k = i; k < j; ++k) {
out += words[k];
if (space > 0) {
int tmp;
if (j == words.size()) {
// the last word
if (j - k == 1) tmp = space;
else tmp = 1;
} else {
if (j - k - 1 > 0) {
if (space % (j - k - 1) == 0) tmp = space / (j - k - 1);
// determine if it can be divided
else tmp = space / (j - k - 1) + 1;
} else tmp = space;// the last words
}
out.append(tmp, ' ');
space -= tmp;
}
}
res.push_back(out);
i = j;
}
return res;
}
};