text-justification
题目描述
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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 exactly maxWidth 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.
Note:
A word is defined as a character sequence consisting of non-space characters only.
Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
The input array words contains at least one word.
思路:
- 从数组中“倒出字串”,包含空格,总长不超过maxWidth。
- 每组的处理:补入空格,生成字串,装入结果切片。
细节:
- 用数组保存间隔空格的个数,轮循++计数。比分3种情况,用switch写的方式更简便。
- range遍历,数组元素要用[]修改。
- 切片使用要初始化,数组不需要。所以每个数组元素赋值用=,不需要用:=。
- 数组初始化,长度必须是常量。如果想用变量,就用切片,make指定len。strSpace = make([]string,len(collect)-1,len(words)。切片不能用range遍历。
调试:
- 在循还体内的一个i:=0子循还,写成了i+=0,导致外层循环的i发生改变,导致数组越界。
- 一个单词要最后一个单词要求左对齐。
- resElem长度是变化的,要先取了来,不能写在循环中
解:
package main
import "fmt"
func fullJustify(words []string, maxWidth int) []string {
var res []string
collect := make([]string, 0, len(words))
collectLength := 0
for i := 0; i < len(words); i++ {
collectLengthMayBe := collectLength
if collectLength == 0 {
collectLengthMayBe += len(words[i])
} else {
collectLengthMayBe += len(words[i]) + 1
}
if collectLengthMayBe > maxWidth {
//handle this group
if len(collect) == 1 {
resElem := collect[0]
for i := 0; i<maxWidth-len(collect[0]);i++ {
resElem += " "
}
res = append(res, resElem)
}else {
//hasSpace
var strSpace []string
strSpace = make([]string, len(collect)-1, len(words))
for i := 0; i < len(strSpace); i++ {
strSpace[i] = " "
}
leftSpace := maxWidth - collectLength
for i := 0; leftSpace > 0; i = (i + 1) % len(strSpace) {
strSpace[i] += " "
leftSpace--
}
resElem := ""
for i := 0; i < len(strSpace); i++ {
resElem = resElem + collect[i] + strSpace[i]
}
resElem += collect[len(collect)-1]
res = append(res, resElem)
}
//reset
collect = make([]string, 0, len(words))
collect = append(collect, words[i])
collectLength = len(words[i])
continue
}
collectLength = collectLengthMayBe
collect = append(collect, words[i])
}
//handle the last group
if len(collect) == 1 {
resElem := collect[0]
for i := 0; i<maxWidth-len(collect[0]);i++ {
resElem += " "
}
res = append(res, resElem)
return res
}
//left-justified
resElem := ""
for i := 0; i < len(collect)-1; i++ {
resElem = resElem + collect[i] + " "
}
resElem += collect[len(collect)-1]
//resElem长度是变化的,要先取了来,不能写在循环中
spaceNum :=maxWidth-len(resElem)
for i := 0; i < spaceNum; i++ {
resElem = resElem + " "
}
res = append(res, resElem)
return res
}
func main() {
words := []string{"What","must","be","acknowledgment","shall","be"}
res := fullJustify(words, 16)
for _,elem:= range res {
fmt.Print(elem,",")
}
fmt.Println()
}