-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#119.cc
More file actions
47 lines (45 loc) · 1.43 KB
/
LeetCode#119.cc
File metadata and controls
47 lines (45 loc) · 1.43 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
44
45
46
47
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int ind = 0;
vector<string> ret;
vector<string> vec;
int len = 0;
while(ind < words.size()){
if(len + words[ind].length() + vec.size() <= L){
vec.push_back(words[ind]);
len+=words[ind].length();
}
else{
if(vec.size()==1){
ret.push_back(vec[0]+string(L-vec[0].length(),' '));
}
else{
string str = "";
int space = L;
for(int i=0;i<vec.size();i++)
space -= vec[i].length();
int num = vec.size()-1;
str+=vec[0];
for(int i=1;i<vec.size();i++){
str+=string(space/num,' ')+(space%num>=i?" ":"");
str+=vec[i];
}
ret.push_back(str);
}
vec.clear();
len = 0;
ind--;
}
ind++;
}
string str = vec[0];
for(int i=1;i<vec.size();i++)
str+=" "+vec[i];
str+=string(L-str.length(),' ');
ret.push_back(str);
return ret;
}
};