给定一个单词数组 words 和一个长度 maxWidth ,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。
你应该使用 “贪心算法” 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ‘ ‘ 填充,使得每行恰好有 maxWidth 个字符。
要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。
文本的最后一行应为左对齐,且单词之间不插入额外的空格。
注意:
- 单词是指由非空格字符组成的字符序列。
- 每个单词的长度大于 0,小于等于 maxWidth。
- 输入单词数组 words 至少包含一个单词。
示例 1:
输入: 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 "
]
提示:
- 1 <= words.length <= 300
- 1 <= words[i].length <= 20
- words[i] 由小写英文字母和符号组成
- 1 <= maxWidth <= 100
- words[i].length <= maxWidth
解题:
func fullJustify(words []string, maxWidth int) []string {
var r []string
l := 0
start := 0
for i, word := range words {
lw := len(word)
if l+lw > maxWidth {
r = append(r, line(words, l-1, start, i, maxWidth))
start = i
l = lw + 1
} else {
l += lw + 1
}
}
last := strings.Join(words[start:], " ")
pad := ""
for i := 0; i < maxWidth-len(last); i++ {
pad += " "
}
last += pad
r = append(r, last)
return r
}
func line(words []string, l, start, end, maxWidth int) string {
padNum := maxWidth - l
space := 0
n := end - start - 1
if n > 0 {
padNum = (maxWidth - l) / n
space = (maxWidth - l) % n
}
pad := ""
for i := 0; i < padNum; i++ {
pad += " "
}
s := words[start]
if n == 0 {
return s + pad
}
for i := start + 1; i < end; i++ {
s += " " + pad
if i-start <= space {
s += " "
}
s += words[i]
}
return s
}
官方解答:
1.模拟
// blank 返回长度为 n 的由空格组成的字符串
func blank(n int) string {
return strings.Repeat(" ", n)
}
func fullJustify(words []string, maxWidth int) (ans []string) {
right, n := 0, len(words)
for {
left := right // 当前行的第一个单词在 words 的位置
sumLen := 0 // 统计这一行单词长度之和
// 循环确定当前行可以放多少单词,注意单词之间应至少有一个空格
for right < n && sumLen+len(words[right])+right-left <= maxWidth {
sumLen += len(words[right])
right++
}
// 当前行是最后一行:单词左对齐,且单词之间应只有一个空格,在行末填充剩余空格
if right == n {
s := strings.Join(words[left:], " ")
ans = append(ans, s+blank(maxWidth-len(s)))
return
}
numWords := right - left
numSpaces := maxWidth - sumLen
// 当前行只有一个单词:该单词左对齐,在行末填充剩余空格
if numWords == 1 {
ans = append(ans, words[left]+blank(numSpaces))
continue
}
// 当前行不只一个单词
avgSpaces := numSpaces / (numWords - 1)
extraSpaces := numSpaces % (numWords - 1)
s1 := strings.Join(words[left:left+extraSpaces+1], blank(avgSpaces+1)) // 拼接额外加一个空格的单词
s2 := strings.Join(words[left+extraSpaces+1:right], blank(avgSpaces)) // 拼接其余单词
ans = append(ans, s1+blank(avgSpaces)+s2)
}
}