-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfullJustify.cpp
57 lines (55 loc) · 1.2 KB
/
fullJustify.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
#include "fullJustify.h"
#include <algorithm>
#include <iostream>
using namespace std;
vector<string> Solution::fullJustify(vector<string> &words, int L) {
vector<string> res;
if(L==0){
res.push_back(string(""));
return res;
}
vector<string> tmp;
int i = 0;
while(1) {
string line = "";
int current_words_length = 0;
int j = i;
while(j< words.size() && (current_words_length + words[j].size() <= L - (j - i))) {
current_words_length += words[j].size();
j++;
}
int num_words = j - i;
if(num_words == 1) {
line.append(words[i]);
line.append(L - words[i].length(), ' ');
i = j;
} else {
if(j==words.size()) {
for (int k = i; k < j; ++k)
{
line.append(words[k]);
if(k<j-1)
line.append(1, ' ');
}
line.append(L - current_words_length - num_words + 1, ' ');
}
else {
for (int k = i; k < j; ++k)
{
line.append(words[k]);
if(k<j-1) {
line.append((L - current_words_length)/(num_words-1), ' ');
if(k-i<(L - current_words_length) % (num_words - 1))
line.append(1, ' ');
}
}
}
i = j;
}
//cout<<line<<endl;
res.push_back(line);
if(i==words.size())
break;
}
return res;
}