-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.h
80 lines (76 loc) · 2.55 KB
/
solution.h
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
76
77
78
79
80
/*
Code generated by https://github.com/goodstudyqaq/leetcode-local-tester
*/
#if __has_include("../utils/cpp/help.hpp")
#include "../utils/cpp/help.hpp"
#elif __has_include("../../utils/cpp/help.hpp")
#include "../../utils/cpp/help.hpp"
#else
#define debug(...) 42
#endif
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
int n = words.size();
vector<string> ans;
int now = 0;
while (now < n) {
int go = now;
int cnt = words[go].size();
go++;
while (go < n && cnt + 1 + words[go].size() <= maxWidth) {
cnt += 1 + words[go].size();
go++;
}
if (cnt == maxWidth) {
string it = "";
for (int i = now; i < go; i++) {
it += words[i];
if (i != go - 1) {
it += ' ';
}
}
ans.push_back(it);
} else {
if (go - now == 1) {
string it = words[now];
while (it.size() != maxWidth) it += ' ';
ans.push_back(it);
} else {
if (go == n) {
string it = words[now];
for (int i = now + 1; i < go; i++) {
it += ' ';
it += words[i];
}
while (it.size() != maxWidth) it += ' ';
ans.push_back(it);
} else {
int num = go - now - 1;
int left = maxWidth - cnt;
int add = left / num;
int more = left % num;
string it;
string space = "";
for (int i = 0; i < add; i++) {
space += ' ';
}
for (int i = now; i < go; i++) {
it += words[i];
if (i != go - 1) {
it += ' ';
it += space;
if (i - now < more) {
it += ' ';
}
}
}
ans.push_back(it);
}
}
}
now = go;
}
return ans;
}
};