leetcode68-文本左右对齐

原题

https://leetcode.cn/problems/text-justification/description

解法

贪心即可

func fullJustify(words []string, maxWidth int) []string {
    index := 0
    count := 0
    var temp []string
    var res []string
    for{
        temp = append(temp, words[index])
        count += len(words[index])
        // 如果已经是最后一个字符串了,直接结束
        if index + 1 == len(words){
            tempStr := strings.Join(temp, " ")
            for len(tempStr) != maxWidth{
                tempStr += " "
            }
            res = append(res, tempStr)
            return res
        }

        // 如果装不下下一个字符串了,则换行
        if count + len(words[index+1]) + len(temp) > maxWidth{
            // 有多少个空格
            spaces := maxWidth - (count)
            tempStr := ""
            if len(temp) == 1{
                tempStr = temp[0] + nSpace(spaces)
            } else{
                // 单词间平均多少个空格
                perSpace := spaces/(len(temp)-1)
                // 余多少个空格
                spareSpace := spaces%(len(temp)-1)

                for index, str := range temp{
                    tempStr += str
                    if index == len(temp) - 1{
                        continue
                    }
                    // 加空格
                    if index < spareSpace{
                        tempStr += nSpace(perSpace+1)
                    } else{
                        tempStr += nSpace(perSpace)
                    }
                }
            }
            res = append(res, tempStr)
            temp = []string{}
            count = 0
        }
        index ++
    }
}

func nSpace(n int) string{
    str := ""
    for i := 0; i < n; i++{
        str += " "
    }
    return str
}

原创文章,作者:彭晨涛,如若转载,请注明出处:https://www.codetool.top/article/leetcode68-%e6%96%87%e6%9c%ac%e5%b7%a6%e5%8f%b3%e5%af%b9%e9%bd%90/

(0)
彭晨涛彭晨涛管理者
上一篇 2024年3月23日
下一篇 2024年3月24日

相关推荐

  • leetcode160-相交链表

    原题 编写一个程序,找到两个单链表相交的起始节点。 如下面的两个链表: 在节点 c1 开始相交。 示例 1: 输入: intersectVal = 8, listA = [4,1,…

    2019年12月14日
    090
  • leetcode496-下一个更大元素I

    原题 给定两个没有重复元素的数组 nums1 和 nums2 ,其中nums1 是 nums2 的子集。找到 nums1 中每个元素在 nums2 中的下一个比其大的值。 nums…

    算法 2020年1月31日
    0400
  • leetcode135-分发糖果

    原题 老师想给孩子们分发糖果,有 N 个孩子站成了一条直线,老师会根据每个孩子的表现,预先给他们评分。 你需要按照以下要求,帮助老师给这些孩子分发糖果: 每个孩子至少分配到 1 个…

    算法 2020年2月17日
    060
  • leetcode542-01矩阵

    原题 给定一个由 0 和 1 组成的矩阵,找出每个元素到最近的 0 的距离。 两个相邻元素间的距离为 1 。 示例1: 输入: 0 0 0 0 1 0 0 0 0 输出: 0 0 …

    2019年12月13日
    080
  • leetcode141-环形链表

    原题 给定一个链表,判断链表中是否有环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没…

    2019年12月14日
    0100
  • leetcode153-寻找旋转排序数组中的最小值

    原题 假设按照升序排序的数组在预先未知的某个点上进行了旋转。 ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。 搜索一个给定的目标值,…

    算法 2020年1月3日
    080
  • leetcode837-新21点

    原题 爱丽丝参与一个大致基于纸牌游戏 “21点” 规则的游戏,描述如下: 爱丽丝以 0 分开始,并在她的得分少于 K 分时抽取数字。 抽取时,她从 [1, W] 的范围中随机获得一…

    算法 2020年6月3日
    0110
  • leetcode263-丑数

    原题 编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。 示例1: 输入: 6 输出: true 解释: 6 = 2 × 3 示例2: 输入: …

    算法 2020年2月6日
    0120
  • leetcode59-螺旋矩阵II

    原题 给定一个正整数 n,生成一个包含 1 到 n2 所有元素,且元素按顺时针顺序螺旋排列的正方形矩阵。 示例: 输入: 3 输出: [ [ 1, 2, 3 ], [ 8, 9, …

    算法 2020年5月13日
    080
  • leetcode151-翻转字符串里的单词

    原题 给定一个字符串,逐个翻转字符串中的每个单词。 示例 1: 输入: “the sky is blue” 输出: “blue is sky the” 示例 2: 输入: “ he…

    算法 2019年11月22日
    090

发表回复

登录后才能评论