首页 > 代码库 > LeetCode: Text Justification [068]
LeetCode: Text Justification [068]
【题目】
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ‘ ‘
when necessary so that each line has exactlyL characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16
.
Return the formatted lines as:
[ "This is an", "example of text", "justification. " ]
Note: Each word is guaranteed not to exceed L in length.
click to show corner cases.
- A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.
【题意】
给定一个单词数组,和一个整数L,要求将字符串中的数组组织成若干行长度为L的字符串,每个字符串中放入尽可能多的单词;
每一行字符串左右对齐,单词之间至少有一个空格间隔,每行中单词之间的间隔尽可能的均匀,如果不能分布均匀,则左边间隔要比右边的间隔来的大;
最后一行左对齐即可,尾部用空格补齐。 【注意,最后一行不管有多少个单词,都是左对齐】
【题目保证单词长度不会大于L,也就是说每行至少有一个单词】
【思路】
依次确定每一行中的单词。
1. 首先在一个空格间隔的情况下尽可能往每一行中放入尽可能多的单词,直到无法再放入更多的单词
2. 将多余的空格分摊到每个间隔上
【代码】
class Solution { public: vector<string> fullJustify(vector<string> &words, int L) { vector<string>result; int size=words.size(); if(size==0 || L==0){ result.push_back(""); return result; } vector<string> wordsInline; //每行中包含的单词 vector<int> seperatesInline; //每个间隔对应的空格 int lineSize=0; //字符串的长度 for(int i=0; i<size; i++){ if(lineSize>0 && lineSize+words[i].length()+1>L){ //当前行已经不能在加入单词了,因此转换成字符串保存,然后开始下一个新串 string line=wordsInline[0]; if(seperatesInline.size()==0){ //如果当前行只能容得下一个单词, 该单词左对齐,剩余部分用空格补齐 string space(L-line.length(), ' '); line += space; } else{ //如果当前行中有间隔, 则将尾部的空格平摊到各个间隔上 int spaceCount = L-lineSize; //尾部的空格数 int seperateCount = seperatesInline.size(); //间隔数 int k=0; while(k<spaceCount){ //平摊 seperatesInline[k%seperateCount]++; k++; } //组合字符串 for(k=0; k<seperateCount; k++){ line += string(seperatesInline[k], ' '); line += wordsInline[k+1]; } } result.push_back(line); wordsInline.clear(); seperatesInline.clear(); lineSize=0; } if(lineSize!=0){seperatesInline.push_back(1); lineSize++;} wordsInline.push_back(words[i]); lineSize+=words[i].length(); } if(wordsInline.size()>0){ //最后一行 string line=wordsInline[0]; if(seperatesInline.size()==0){ //如果当前行只能容得下一个单词, 该单词左对齐,剩余部分用空格补齐 string space(L-line.length(), ' '); line += space; } else{ //如果当前行中有间隔, 则将尾部的空格平摊到各个间隔上 int spaceCount = L-lineSize; //尾部的空格数 int seperateCount = seperatesInline.size(); //间隔数 int k=0; //组合字符串 for(k=0; k<seperateCount; k++){ line += string(seperatesInline[k], ' '); line += wordsInline[k+1]; } line += string(spaceCount, ' '); //补齐尾部的空格 } result.push_back(line); } result; } };