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.
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.
文本调整:
给又一组字符串组成的文本和一个maxWidth,要求将文本调整成每行为maxWidth个字符的格式。
具体调整时是左右对齐(两端对齐)的,且字符串之间由空格符‘ ‘分开,要求空格符尽量均匀, 若不能均匀,左边空格多于右边空格
对于最后一行,要求左对齐,且字符串间不插入额外字符【之前理解错了,是不插入额外字符,还是要插入空白符的!!】
注意: 若在除最后一行的某一行只包括一个单词,该怎么办!!!leetcode的提示——左对齐即可
思路:
使用字符串tmp记录当前行
1、计算能否加入下一个单词word
2、若长度L能容纳,则加入tmp,并更新tmp长度number
3、若长度L不能容纳,则line即为符合条件的一行,并重新调整单词间隔,构成一行,并插入result
4、单词全部添加完毕
注意:添加最后一行
难点:思路上面敢想敢动手就可以,没有太大难度,但是,就差在细节方面,这题做了接近三个小时,难吗?真心不难,但,就是一些细节上有问题,再加上笔误,调试花费很长时间,关键,还是要细心,多考虑到各种情况,再加上多练,速度才能提上来。
另外,题意的理解也是一个问题,有的确实是自己理解的问题,有的,则是题意存在歧义,得提交之后根据题意再改。
1 #include "stdafx.h" 2 #include <string> 3 #include <iostream> 4 #include <vector> 5 6 using namespace std; 7 8 9 class Solution 10 { 11 public: 12 vector<string> fullJustify(vector<string> &words, int maxWidth) 13 { 14 /* 15 文本调整: 16 给又一组字符串组成的文本和一个maxWidth,要求将文本调整成每行为maxWidth个字符的格式。 17 具体调整时是左右对齐(两端对齐)的,且字符串之间由空格符‘ ‘分开,要求空格符尽量均匀, 18 若不能均匀,左边空格多于右边空格 19 对于最后一行,要求左对齐,且字符串间不插入额外字符【之前理解错了,是不插入额外字符,还是要插入空白符的!!】 20 21 注意:若在除最后一行的某一行只包括一个单词,该怎么办!!!leetcode的提示 22 23 思路: 24 依次读取数组中的单词,计算其大小,若<maxWidth,累加其大小,并+1(空格的大小),直到>maxWidth, 25 然后根据读取的个数,合理分配空格符的位置和大小 26 */ 27 if(words.size() == 0 || maxWidth <= 0)//具体该返回空还是返回原来的,该怎么算?看题意! 28 return words; 29 30 vector<string> result;//存结果的vector 31 int start = 0; 32 int wordCount = 1; 33 int number = 0; 34 int spaceCount = 0;//本行每处平均空白符的个数 35 int extraSpaceCount = 0;//若本行的空白符不能平均分配,第一个空白处比其他空白处多出来的空白符的个数 36 int i=0; 37 while(i<words.size()) 38 { 39 string tmp;//记录当前行的string结构 40 start = i;//记录当前行开始的单词下标 41 wordCount = 1;//记录当前行包括的单词数 42 43 number = words[i].size();//记录累加的字符数 44 while(number <= maxWidth && i<words.size())//对于循环,后一个条件一定要加 45 { 46 if( (i<words.size()-1) && (number+words[i+1].size()+1 <= maxWidth) )//保证不越界 47 { 48 wordCount++; 49 number += words[i+1].size()+1; 50 i++;//i最后指向本行的最后一个单词 51 } 52 else 53 break;//之前没加,一直循环,无法终止 54 } 55 56 if(i == words.size()-1)//最后一行的判断条件之前有问题,最后一个肯定在最后一行 57 {//最后一行要单独处理!每个单词间有一个空格即可 58 //(也不能没有!!!,我觉得leetcode题意交代有问题!!!) 59 for(int j=start; j<=i; j++) 60 { 61 if(j != i) 62 tmp += words[j] + ‘ ‘; 63 else 64 tmp += words[j]; 65 } 66 spaceCount = maxWidth-number; 67 if(spaceCount != 0) 68 tmp += string(spaceCount,‘ ‘); 69 result.push_back(tmp); 70 break;//又忘了!! 71 } 72 //此时,number中已经包括空白符wordCount-1个 73 //注:特殊情况: wordCount为1时,左对齐,空格放在后面 74 if(wordCount == 1)//此时必为中间行 75 { 76 tmp += words[start]; 77 if(number<maxWidth) 78 tmp += string(maxWidth-number,‘ ‘); 79 result.push_back(tmp); 80 i++;//又漏了!! 81 continue; 82 } 83 spaceCount = (maxWidth-number)/(wordCount-1) +1; 84 extraSpaceCount = (maxWidth-number)%(wordCount-1); 85 86 //题意理解问题:不能均分的空格符,依次从前往后加,而不是全都加到第一个上面!!! 87 for(int j=start; j<=i; j++) 88 { 89 // if(j == start && extraSpaceCount != 0) 90 // tmp += words[j] + string(spaceCount+extraSpaceCount,‘ ‘);//初始化结构弄错了!!! 91 // else 92 // { 93 // if(j != i) 94 // tmp += words[j] + string(spaceCount,‘ ‘); 95 // else 96 // tmp += words[j]; 97 // } 98 if(j != i) 99 { 100 if(extraSpaceCount != 0) 101 { 102 tmp += words[j] + string(spaceCount+1,‘ ‘); 103 extraSpaceCount--; 104 } 105 else 106 tmp += words[j] + string(spaceCount,‘ ‘); 107 } 108 else 109 tmp += words[j]; 110 } 111 result.push_back(tmp); 112 i++;//勿忘! 113 } 114 return result; 115 } 116 }; 117 118 119 int main() 120 { 121 Solution sol; 122 //测试用例1 123 string data[] = {"This", "is", "an", "example", "of", "text", "justification."}; 124 vector<string> test(data,data+7); 125 vector<string> result = sol.fullJustify(test,16); 126 for(auto const &i : result) 127 cout << i << endl; 128 cout<<endl; 129 130 //测试用例2 131 string data1[] = {"This","is","a","justification","of","special","example."}; 132 vector<string> test1(data1,data1+7); 133 result = sol.fullJustify(test1,15);//测试中间行只有一个单词 134 for(auto const &i : result) 135 cout << i << endl; 136 cout<<endl; 137 138 //测试用例3 139 string data2[] = {"What","must","be","shall","be."}; 140 vector<string> test2(data2,data2+5); 141 result = sol.fullJustify(test2,12);//测试中间行只有一个单词 142 for(auto const &i : result) 143 cout << i << endl; 144 cout<<endl; 145 146 //测试用例4 147 string data3[] = {"My","momma","always","said,","Life","was","like","a","box","of","chocolates.","You","never","know","what","you‘re","gonna","get."}; 148 vector<string> test3(data3,data3+18); 149 result = sol.fullJustify(test3,20);//测试中间行只有一个单词 150 for(auto const &i : result) 151 cout << i << endl; 152 cout<<endl; 153 154 /* 155 提交错误实例: 156 1、因为题意理解问题,最后一行单词间也是要有一个空格的,我理解为不需要 157 2、空格分配问题:不能均分的空格符,依次从前往后加,而不是全都加到第一个上面!!! 158 */ 159 160 //测试用例5 161 /* 162 Input: ["Don‘t","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."], 30 163 Output: ["Don‘t go around saying the","world owes you a living; the","world owes you nothing; it was","here first. "] 164 Expected:["Don‘t go around saying the","world owes you a living; the","world owes you nothing; it was","here first. "] 165 */ 166 string data4[] = {"Don‘t","go","around","saying","the","world","owes","you","a","living;","the","world","owes","you","nothing;","it","was","here","first."}; 167 vector<string> test4(data4,data4+19); 168 result = sol.fullJustify(test4,30);//测试中间行只有一个单词 169 for(auto const &i : result) 170 cout << i << endl; 171 cout<<endl; 172 173 return 0; 174 }