text-justification go语言实现

2018-09-22  本文已影响0人  fjxCode

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.

思路:

细节:

调试:

解:

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()
}
上一篇 下一篇

猜你喜欢

热点阅读