-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextJustification.h
85 lines (85 loc) · 2.53 KB
/
TextJustification.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
81
82
83
84
85
/**
* Solution 1
* time: O(n) space: O(1)
* 细节实现题,要考虑周全,有很多corner case。代码太丑,还需要再优化。
*/
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L) {
if(L<1)return words;
vector<string> ret;
int szLine=L,szVec=words.size();
string str("");
int start=0;
for(int i=0;i<szVec;++i)
{
int szWord=words[i].size();
if(szLine>szWord)
{
if(i==szVec-1)
{
if(start==i)
{
string s(szLine-szWord,' ');
str=words[i]+s;
ret.push_back(str);
return ret;
}
for(int j=start;j<=i;++j)str+=words[j];
szLine-=words[i].size();
if(szLine>0)str+=string(szLine,' ');
ret.push_back(str);
return ret;
}
szLine-=szWord+1;
words[i]+=' ';
}
else if(szLine==szWord)
{
for(int j=start;j<=i;++j)str+=words[j];
ret.push_back(str);
start=i+1;
str="";
szLine=L;
}
else
{
++szLine;
words[i-1].erase(words[i-1].end()-1);
int num=i-start;
bool flag=false;
if(num>1)
{
--num;
flag=true;
}
int rem=szLine%num;
int shang=szLine/num;
int j=0;
if(rem>0)
{
string s1(1+shang,' ');
for(;j<rem;++j)str+=words[start+j]+s1;
}
string s2(shang,' ');
for(;j<num;++j)str+=words[start+j]+s2;
if(flag)str+=words[start+j];
ret.push_back(str);
str="";
szLine=L-szWord-1;
start=i;
words[i]+=' ';
if(i==szVec-1)
{
++szLine;
words[i].erase(words[i].end()-1);
str+=words[i];
if(szLine>0)str+=string(szLine,' ');
ret.push_back(str);
return ret;
}
}
}
return ret;
}
};