-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextJustification.cpp
More file actions
43 lines (41 loc) · 1.22 KB
/
textJustification.cpp
File metadata and controls
43 lines (41 loc) · 1.22 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
41
42
43
// Source: https://leetcode.com/problems/text-justification/
// Author: Miao Zhang
// Date: 2021-01-13
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> res;
vector<string> cur;
int cnt = 0;
for (auto word: words) {
if (cnt + cur.size() + word.size() > maxWidth) {
for (int i = 0; i < maxWidth - cnt; i++) {
int base;
if (cur.size() == 1) base = 1;
else base = cur.size() - 1;
cur[i % base] += ' ';
}
string tmp;
for(auto c: cur) {
tmp += c;
}
res.push_back(tmp);
cur.clear();
cnt = 0;
}
cur.push_back(word);
cnt += word.size();
}
string tmp;
for (int i = 0; i < cur.size(); i++) {
if (i != cur.size() - 1) {
cur[i] += ' ';
}
tmp += cur[i];
}
int n = maxWidth - tmp.size();
for (int i = 0; i < n; i++) tmp += " ";
res.push_back(tmp);
return res;
}
};