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.
String
思路:
计算多个word 时空格的情况,通过记录空格的个数spaceLeft,还有位置个数spaceAddr,(spaceLeft+spaceAddr-1)/spaceAddr,则为最左边的位置空格个数,然后更新 spaceLeft 和 spaceAddr,这样就可以在行添加word 时候把words 计算进去。
我写的代码:
1 #include <string> 2 #include <vector> 3 #include <iostream> 4 using namespace std; 5 6 class Solution { 7 public: 8 vector<string> fullJustify(vector<string> &words, int L) { 9 int curLen = 0,startIdx = 0,curWordIdx =-1,wordLen =0; 10 int spaceLeft=0,spaceAddr=0,temp=0; 11 vector<string> ret; 12 string line=""; 13 while(++curWordIdx<words.size()){ 14 if(curLen+words[curWordIdx].size()<=L){ 15 curLen+=words[curWordIdx].size()+1; 16 wordLen+= words[curWordIdx].size(); 17 continue; 18 } 19 if(startIdx+1==curWordIdx) 20 line=words[startIdx]+string(L-words[startIdx].size(),' '); 21 else{ 22 spaceLeft=L - wordLen; 23 spaceAddr = curWordIdx - startIdx -1; 24 line = words[startIdx]; 25 // cout<<spaceLeft<<" "<<spaceAddr<<endl; 26 while(++startIdx<curWordIdx){ 27 temp = (spaceLeft+spaceAddr-1)/spaceAddr; 28 line+=string(temp,' '); 29 line+=words[startIdx]; 30 spaceLeft-=temp; 31 spaceAddr--; 32 } 33 } 34 ret.push_back(line); 35 spaceLeft = spaceAddr =wordLen= curLen= 0; 36 startIdx = curWordIdx; 37 curWordIdx --; 38 line=""; 39 } 40 if(curLen>0){ 41 42 line = words[startIdx]; 43 while(++startIdx<curWordIdx){ 44 line+=" "+words[startIdx]; 45 } 46 line+=string(L-line.size(),' '); 47 ret.push_back(line); 48 } 49 return ret; 50 } 51 }; 52 53 int main() 54 { 55 vector<string> words={"What","must","be","shall","be."}; 56 int l = 12; 57 Solution sol; 58 vector<string> ret = sol.fullJustify(words,l); 59 for(int i =0;i<ret.size();i++) 60 cout<<ret[i]<<"*"<<endl; 61 62 63 return 0; 64 }
1 #include <string> 2 #include <vector> 3 #include <iostream> 4 using namespace std; 5 /** 6 class Solution { 7 public: 8 vector<string> fullJustify(vector<string> &words, int L) { 9 int curLen = 0,startIdx = 0,curWordIdx =-1,wordLen =0; 10 int spaceLeft=0,spaceAddr=0,temp=0; 11 vector<string> ret; 12 string line=""; 13 while(++curWordIdx<words.size()){ 14 if(curLen+words[curWordIdx].size()<=L){ 15 curLen+=words[curWordIdx].size()+1; 16 wordLen+= words[curWordIdx].size(); 17 continue; 18 } 19 if(startIdx+1==curWordIdx) 20 line=words[startIdx]+string(L-words[startIdx].size(),' '); 21 else{ 22 spaceLeft=L - wordLen; 23 spaceAddr = curWordIdx - startIdx -1; 24 line = words[startIdx]; 25 // cout<<spaceLeft<<" "<<spaceAddr<<endl; 26 while(++startIdx<curWordIdx){ 27 temp = (spaceLeft+spaceAddr-1)/spaceAddr; 28 line+=string(temp,' '); 29 line+=words[startIdx]; 30 spaceLeft-=temp; 31 spaceAddr--; 32 } 33 } 34 ret.push_back(line); 35 spaceLeft = spaceAddr =wordLen= curLen= 0; 36 startIdx = curWordIdx; 37 curWordIdx --; 38 line=""; 39 } 40 if(curLen>0){ 41 42 line = words[startIdx]; 43 while(++startIdx<curWordIdx){ 44 line+=" "+words[startIdx]; 45 } 46 line+=string(L-line.size(),' '); 47 ret.push_back(line); 48 } 49 return ret; 50 } 51 }; 52 */ 53 class Solution { 54 public: 55 vector<string> fullJustify(vector<string> &words, int L) { 56 vector<string> ret; 57 for(int i=0,k,len;i<words.size();i+=k){ 58 for( k=len=0;i+k<words.size()&&len+words[i+k].size()+k<=L;k++) 59 len+=words[i+k].size(); 60 string temp = words[i]; 61 for(int j=0;j<k-1;j++){ 62 if(i+k>=words.size()) temp+=" ";//for the last line. 63 else temp+=string((L-len)/(k-1)+(j<( (L-len)%(k-1) )),' ' ); 64 temp+=words[i+j+1]; 65 } 66 temp+=string(L-temp.size(),' '); 67 ret.push_back(temp); 68 } 69 return ret; 70 } 71 }; 72 73 int main() 74 { 75 vector<string> words={"What","must","be","shall","be."}; 76 int l = 12; 77 Solution sol; 78 vector<string> ret = sol.fullJustify(words,l); 79 for(int i =0;i<ret.size();i++) 80 cout<<ret[i]<<"*"<<endl; 81 82 83 return 0; 84 }