-
Notifications
You must be signed in to change notification settings - Fork 548
/
Copy pathtext-justification.go
executable file
·73 lines (58 loc) · 1.41 KB
/
text-justification.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package Problem0068
import "strings"
func fullJustify(words []string, maxWidth int) []string {
res := []string{}
temp := []string{}
width := 0
isLast := false
for !isLast {
words, temp, width, isLast = split(words, maxWidth)
res = append(res, combine(temp, width, maxWidth, isLast))
}
return res
}
// 返回不组合的单词,需要组合的单词,和这些单词的长度之和, 是否是结尾
func split(words []string, maxWidth int) ([]string, []string, int, bool) {
temp := make([]string, 1)
temp[0] = words[0]
width := len(words[0])
i := 1
for ; i < len(words); i++ {
if width+len(temp)+len(words[i]) > maxWidth {
break
}
temp = append(temp, words[i])
width += len(words[i])
}
return words[i:], temp, width, i == len(words)
}
func combine(words []string, width, maxWidth int, isLast bool) string {
wordCount := len(words)
if wordCount == 1 || isLast {
return combineSpecial(words, maxWidth)
}
spaceCount := wordCount - 1
spaces := makeSpaces(spaceCount, maxWidth-width)
res := ""
for i, v := range spaces {
res += words[i] + v
}
if wordCount > 1 {
res += words[wordCount-1]
}
return res
}
func makeSpaces(Len, count int) []string {
res := make([]string, Len)
for i := 0; i < count; i++ {
res[i%Len] += " "
}
return res
}
func combineSpecial(words []string, maxWidth int) string {
res := strings.Join(words, " ")
for len(res) < maxWidth {
res += " "
}
return res
}