68 Text Justification 发表于 2016-12-14 | 分类于 LeetCode | | 阅读次数: 解题思路 将单词分组打出,每组长度为L; 除了最后一行,单词需要左右对齐,平铺效果,中间以空格均匀填充; 最后一行,总长度也是L,但是要求左对齐,以一个空格隔开即可; 代码123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566class Solution{ public: vector<string> fullJustify(vector<string> &words, int maxWidth) { int len = words.size(); vector<string> res; vector<string> temp; string str; int count = 0; int totalLen = 0; for(int i = 0; i < len; ++i) { if(count + totalLen + words[i].size() > maxWidth) { str = ""; str += temp[0]; if(count == 1) { str += string(maxWidth - totalLen, ' '); } else if(count == 2) { str += string(maxWidth - totalLen, ' '); str += temp[1]; } else { int block = (maxWidth - totalLen) / (count - 1); int last = maxWidth - totalLen - block * (count - 1); for(int j = 1; j < count; ++j) { str += string(block, ' '); if(last-- > 0) str += ' '; str += temp[j]; } } res.push_back(str); temp.clear(); temp.push_back(words[i]); count = 1; totalLen = words[i].size(); } else { temp.push_back(words[i]); count++; totalLen += words[i].size(); } } if(count) { str = ""; str += temp[0]; for(int j = 1; j < temp.size(); ++j) { str += ' '; str += temp[j]; } str += string(maxWidth - totalLen - count + 1, ' '); res.push_back(str); } return res; }}; -------------本文结束感谢您的阅读-------------