**输入:** words = ["This", "is", "an", "example", "of", "text", "justification."], maxWidth = 16
**输出:**
[
"This is an",
"example of text",
"justification. "
]
示例 2:
**输入:** words = ["What","must","be","acknowledgment","shall","be"], maxWidth = 16
**输出:**
[
"What must be",
"acknowledgment ",
"shall be "
]
**解释:** 注意最后一行的格式应为 "shall be " 而不是 "shall be",
因为最后一行应为左对齐,而不是左右两端对齐。
第二行同样为左对齐,这是因为这行只包含一个单词。
示例 3:
**输入:** words = ["Science","is","what","we","understand","well","enough","to","explain","to","a","computer.","Art","is","everything","else","we","do"],maxWidth = 20
**输出:**
[
"Science is what we",
"understand well",
"enough to explain to",
"a computer. Art is",
"everything else we",
"do "
]
# blank 返回长度为 n 的由空格组成的字符串 defblank(n: int) -> str: return' ' * n
classSolution: deffullJustify(self, words: List[str], maxWidth: int) -> List[str]: ans = [] right, n = 0, len(words) whileTrue: left = right # 当前行的第一个单词在 words 的位置 sumLen = 0# 统计这一行单词长度之和 # 循环确定当前行可以放多少单词,注意单词之间应至少有一个空格 while right < n and sumLen + len(words[right]) + right - left <= maxWidth: sumLen += len(words[right]) right += 1
# 当前行是最后一行:单词左对齐,且单词之间应只有一个空格,在行末填充剩余空格 if right == n: s = " ".join(words[left:]) ans.append(s + blank(maxWidth - len(s))) break
numWords = right - left numSpaces = maxWidth - sumLen
# 当前行只有一个单词:该单词左对齐,在行末填充空格 if numWords == 1: ans.append(words[left] + blank(numSpaces)) continue
// join 返回用 sep 拼接 [left, right) 范围内的 words 组成的字符串 string join(vector<string> &words, int left, int right, string sep){ string s = words[left]; for (int i = left + 1; i < right; ++i) { s += sep + words[i]; } return s; }
public: vector<string> fullJustify(vector<string> &words, int maxWidth){ vector<string> ans; int right = 0, n = words.size(); while (true) { int left = right; // 当前行的第一个单词在 words 的位置 int sumLen = 0; // 统计这一行单词长度之和 // 循环确定当前行可以放多少单词,注意单词之间应至少有一个空格 while (right < n && sumLen + words[right].length() + right - left <= maxWidth) { sumLen += words[right++].length(); }
// 当前行是最后一行:单词左对齐,且单词之间应只有一个空格,在行末填充剩余空格 if (right == n) { string s = join(words, left, n, " "); ans.emplace_back(s + blank(maxWidth - s.length())); return ans; }
int numWords = right - left; int numSpaces = maxWidth - sumLen;
classSolution { public List<String> fullJustify(String[] words, int maxWidth) { List<String> ans = newArrayList<String>(); intright=0, n = words.length; while (true) { intleft= right; // 当前行的第一个单词在 words 的位置 intsumLen=0; // 统计这一行单词长度之和 // 循环确定当前行可以放多少单词,注意单词之间应至少有一个空格 while (right < n && sumLen + words[right].length() + right - left <= maxWidth) { sumLen += words[right++].length(); }
// 当前行是最后一行:单词左对齐,且单词之间应只有一个空格,在行末填充剩余空格 if (right == n) { StringBuffersb= join(words, left, n, " "); sb.append(blank(maxWidth - sb.length())); ans.add(sb.toString()); return ans; }
intnumWords= right - left; intnumSpaces= maxWidth - sumLen;
publicclassSolution { public IList<string> FullJustify(string[] words, int maxWidth) { IList<string> ans = new List<string>(); int right = 0, n = words.Length; while (true) { int left = right; // 当前行的第一个单词在 words 的位置 int sumLen = 0; // 统计这一行单词长度之和 // 循环确定当前行可以放多少单词,注意单词之间应至少有一个空格 while (right < n && sumLen + words[right].Length + right - left <= maxWidth) { sumLen += words[right++].Length; }
int numWords = right - left; int numSpaces = maxWidth - sumLen;
// 当前行只有一个单词:该单词左对齐,在行末填充剩余空格 if (numWords == 1) { StringBuilder sb = new StringBuilder(words[left]); sb.Append(Blank(numSpaces)); ans.Add(sb.ToString()); continue; }
// 当前行不只一个单词 int avgSpaces = numSpaces / (numWords - 1); int extraSpaces = numSpaces % (numWords - 1); StringBuilder curr = new StringBuilder(); curr.Append(Join(words, left, left + extraSpaces + 1, Blank(avgSpaces + 1))); // 拼接额外加一个空格的单词 curr.Append(Blank(avgSpaces)); curr.Append(Join(words, left + extraSpaces + 1, right, Blank(avgSpaces))); // 拼接其余单词 ans.Add(curr.ToString()); } }
// Blank 返回长度为 n 的由空格组成的字符串 publicstringBlank(int n) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < n; ++i) { sb.Append(' '); } return sb.ToString(); }
// Join 返回用 sep 拼接 [left, right) 范围内的 words 组成的字符串 public StringBuilder Join(string[] words, int left, int right, string sep) { StringBuilder sb = new StringBuilder(words[left]); for (int i = left + 1; i < right; ++i) { sb.Append(sep); sb.Append(words[i]); } return sb; } }
constfullJustify = (words, maxWidth) => { const ans = []; let right = 0, n = words.length; while (true) { const left = right; // 当前行的第一个单词在 words 的位置 let sumLen = 0; // 统计这一行单词长度之和 while (right < n && sumLen + words[right].length + right - left <= maxWidth) { sumLen += words[right].length; right++; }