leetcode刷题笔记六十八题 文本左右对齐

leetcode刷题笔记六十八题 文本左右对齐

leetcode刷题笔记六十八题 文本左右对齐

源地址:68. 文本左右对齐

问题描述:

给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。

要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

文本的最后一行应为左对齐,且单词之间不插入额外的空格。

说明:

单词是指由非空格字符组成的字符序列。
每个单词的长度大于 0,小于等于 maxWidth。
输入单词数组 words 至少包含一个单词。
示例:

输入:
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 "
]

代码补充:

/**
本题需要考虑的细节较多
ans => 存储结果
last => 本层最初的位置
cur => 本层当前位置
floorLength => 本层已存储词(词后会保留一个空格)
视当前词能否放入分为两种情况
1.当前词能够放入,更新cur和floorLength
2.当前词不能够放入, 这种情况分为单个词和多个词处理
最后需要考虑的最后一行的处理
*/
import scala.collection.mutable
object Solution {
    def fullJustify(words: Array[String], maxWidth: Int): List[String] = {
        val ans = mutable.ListBuffer[String]()
        var last = 0
        var cur = 1
        var floorLength = words(0).length
        

        while(cur < words.length && floorLength <= maxWidth){
            val tempLength = floorLength + words(cur).length + 1
            println(tempLength)
            if (tempLength <= maxWidth){
                cur += 1
                floorLength = tempLength
            }
            else{
                val buckets = cur - last
                val space = maxWidth - floorLength
                if (buckets == 1){
                    ans += (words(last) + " "*space)
                }
                else{
                    //quo为平均分配的每个词后的空格
                    val quo = space/(buckets-1)
                    //rem为分配后剩余的空格
                    val rem = space%(buckets-1)
                    val str = new StringBuilder
					//将rem中剩余的空格一一分配给左边的词
                    for(i <- 0 until rem) str ++= (words(last+i) + " " *(quo+2))
                    //右边的词只添加quo和计算中默认的一个空格
                    for(i <- rem until buckets-1) str ++= (words(last+i) + " "*(quo+1))
                    //将最后一个词挂上
                    str ++= words(cur-1)
                    ans += str.toString
                }
                //当前层次处理完 更新结果
                last = cur
                cur = last + 1
                floorLength = words(last).length
            }
        }
        
        //lastLine
        //floorLength不为0的情况,说明为最后一层
        //将所有词切片其后放入空格,再在尾部添加满足maxWidth的空格
        if (floorLength > 0){
            val temp = words.slice(last ,words.length).mkString(" ")
            ans += (temp + " " * (maxWidth - temp.length))
        }

        //test
        return ans.toList
    }
}