0
点赞
收藏
分享

微信扫一扫

68. Text Justification

sunflower821 2023-01-11 阅读 68


class Solution {
public:
bool is_integer(int rest,int distance){
int int_res=rest/distance;
double double_res=(double)rest/distance;
if(double_res==int_res)return true;
else return false;
}
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string>res;
for(int i=0;i<words.size();++i){
string temp_str=words[i];
int Cnt=words[i].length();
int rest=maxWidth-Cnt;
int next_i=i+1;
while(Cnt+words[next_i].length()+1<=maxWidth){
Cnt=Cnt+words[next_i].length()+1;
rest=maxWidth-Cnt;
++next_i;
}
if(next_i<words.size()){
for(int j=i+1;j<next_i;++j){
int current=next_i-j;
int d=is_integer(rest,current)?(rest/current):(rest/current+1);
rest-=d;
for(int i=0;i<=d;++i)temp_str+=" ";
temp_str+=words[j];
}
}
else {
for(int j=i+1;j<next_i;++j){
temp_str+=" ";
temp_str+=words[j];
}
}
int d=maxWidth-temp_str.length();
for(int j=0;j<d;++j)temp_str+=" ";
res.push_back(temp_str);
i=next_i-1;
}
return res;
}
};


举报

相关推荐

0 条评论