leetcode 68:文字左右對齊
阿新 • • 發佈:2018-12-09
原理很簡單,但是寫起來比較複雜,首先將不超過maxWidth的字串放入一個新的陣列result,不要忽略空格所佔空間
對result進行處理,首先去除字串最後面的一個或多個空格,然後計算去除空格後的字串大小,用過最大大小減去去除空格之後的字串大小,得到d 用除以單詞個數-1 即為原來每個空格位置需要插入的空格個數
對於只有一個單詞的字串和最後一個字串只需要在最後面補足夠的空格即可
std::vector<std::string> fullJustify(std::vector<std::string>& words, int maxWidth) { std::vector<std::string> result; std::vector<std::string> d1; std::vector<std::vector<std::string>> d2; std::string s1=""; int i=0; while(i!=words.size()) { s1=""; d1.clear(); while((words[i].size()+s1.size())<=maxWidth) { s1+=words[i]; s1+=" "; d1.push_back(words[i]); i++; } d2.push_back(d1); result.push_back(s1); } std::string s2=""; for(int i=0;i<result.size()-1;i++){ s2=""; result[i].erase(result[i].find_last_not_of(" ")+1); if(result[i].find_first_of(" ")==-1){ int t1=result[i].size(); for(int j=0;j<maxWidth-t1;j++) { result[i]+=" "; } continue; } int c=d2[i].size()-1; int d=maxWidth-result[i].size(); int x1=d/c; int x2=d%c; s2=d2[i][0]; for(int j=1;j<d2[i].size();j++) { if(j<x2+1){ for(int k=0;k<x1+2;k++){ s2+=" "; } } else { for(int k=0;k<x1+1;k++){ s2+=" "; } } s2+=d2[i][j]; } result[i]=s2; } result[result.size()-1].erase(result[result.size()-1].find_last_not_of(" ")+1); int t=result[result.size()-1].size(); int d=maxWidth-t; for(int i=0;i<d;i++) result[result.size()-1]+=" "; return result; }