-
Bingo
Java :
public class Solution { public List<String> fullJustify(String[] words, int L) { List<String> ans = new ArrayList<String>(); int n = words.length; int i = 0; while (i < n) { int len = words[i].length(); int j = i + 1; while (j < n && len + 1 + words[j].length() <= L) { len += 1 + words[j].length(); j++; } String line = words[i]; if (j == n) { // if this is the last line for (int k = i + 1; k < n; k++) { line += " " + words[k]; } while (line.length() < L) { line += " "; } } else { int extraWhite = L - len; int whiteNum = j - i - 1; if (whiteNum == 0) { // if this line has only one word while (line.length() < L) { line += " "; } } else { for (int k = i + 1; k < j; k++) { line += " "; for (int p = 0; p < extraWhite/whiteNum; p++) { line += " "; } if (k - i <= extraWhite%whiteNum) { line += " "; } line += words[k]; } } } ans.add(line); i = j; } return ans; } }
-
Bingo
C++:
class Solution { public: vector<string> fullJustify(vector<string> &words, int L) { vector<string> res; int i = 0; while (i < words.size()) { int j = i, len = 0; while (j < words.size() && len + words[j].size() + j - i <= L) { len += words[j++].size(); } string out; int space = L - len; for (int k = i; k < j; ++k) { out += words[k]; if (space > 0) { int tmp; if (j == words.size()) { if (j - k == 1) tmp = space; else tmp = 1; } else { if (j - k - 1 > 0) { if (space % (j - k - 1) == 0) tmp = space / (j - k - 1); else tmp = space / (j - k - 1) + 1; } else tmp = space; } out.append(tmp, ' '); space -= tmp; } } res.push_back(out); i = j; } return res; } };
-