【算法】Text Justification 文本对齐

ENVI 5.3与IDL 8.5安装指南:从下载到激活的完整流程 本文提供了一份详细的ENVI 5.3与IDL 8.5安装教程,涵盖从获取安装包、运行主程序、安装许可证到关键文件替换的完整流程。指南重点解决了激活过程中的常见问题,并分享了个人实践经验,旨在帮助用户,特别是遥感图像处理初学者,一次性顺利完成软件部署,为后续的科研与开发工作奠定基础。 阅读详情

【算法】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.
Example 1:

Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Example 2:

Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]

Explanation: Note that the last line is "shall be " instead of “shall be”,
because the last line must be left-justified instead of fully-justified.
Note that the second line is also left-justified becase it contains only one word.

Example 3:

Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
         "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

给出一组单词 words ,一个长度 maxWidth ,返回一个字符串数组满足如下条件:

  1. 所有字符长度均为 maxWidth
  2. 单词不可切割
  3. 每行字符串中,每个单词之间的空格大于等于 1 ,且均等分
  4. 如无法均等分,则左边的空格数比右边的多
  5. 最后一行单词之间只有一个空格,剩余空间用空格补齐

解题思路

主要就是计算每行字符串所需的单词 索引范围 和 空格数量 ,最后一行的处理也有些不同:

  1. 动态字符串长度 strCount,头位置 from ,尾位置 to,单词个数 wordCount = to - from + 1
  2. 判断条件就是 strCount + (当前单词长度)word.count + wordCount - 1 > mixWidth 时,from ~ to-1 就是符合条件的单词索引范围
  3. from ~ to-1 单词数量判断空格区间有几个:
    • 单词数量<3时有一个,空格长度为 maxWidth - word.count,添加到单词后面
    • 单词数量>3时有 单词数量 - 1 个
    • 较大空格数量 larCount = (maxWidth - wordsLen) % (wordsCount - 1)
    • 空格长度 spaceLen = (maxWidth - wordsLen) / (wordsCount - 1)
    • 头 larCount 个空格区域有 spaceLen+1,之后都是 spaceLen 个空格
    • 最后一个单词后面注意不添加空格
  4. 最后一行单词和空格交叉添加,最后位数不足用空格补齐

代码实现

Runtime: 8 ~ 20 ms
Memory: 21 MB

func fullJustify(_ words: [String], _ maxWidth: Int) -> [String] {
        //添加字符串的数组
        var result = [String]()
        //动态字符串长度 strCount,头位置 from 尾位置to
        var strCount = 0, from = 0, to = 0
        //循环条件,to 在 words 范围内
        while to < words.count {
            // 空格数 spaceCount = to - from
            // 字符数 + spaceCount > maxWidth , 或者 to 不在 words 范围中时,跳出循环,
            while to < words.count && strCount + words[to].count + to - from <= maxWidth {
                //循环条件内,添加动态字符串长度
                strCount += words[to].count
                //to 后移一位
                to += 1;
            }
            // to 指向最后一个单词,应该跳出循环处理最后一行字符串
            if to == words.count {
                break
            }
            // from ~ to-1 的单词加上空格数满足条件,进行处理
            let strEl = self.fillSpace(words, maxWidth: maxWidth, from: from, to: to - 1, wordsLen: strCount)
            //将处理后的字符串添加进 result 中
            result.append(strEl)
            // 将 from 指向 to
            from = to
            // 重置 strCount 为 0
            strCount = 0
        }
        //处理最后一行字符串
        let strEl = self.fillLastLine(words, maxWidth: maxWidth, from: from, to: to - 1, wordsLen: strCount)
        result.append(strEl)
        //返回结果
        return result
    }

    func fillSpace(_ words: [String], maxWidth: Int, from: Int, to: Int, wordsLen: Int) -> String{
        var resStr = ""
        let space = "*"
        //单词数量 wordsCount
        let wordsCount = to - from + 1
        //较大空格的数量 larCount
        var larCount = 0
        //空格的长度 spaceLen
        var spaceLen : Int = 1
        if wordsCount<3 {
            //单词数量小于3时,larCount=0,spaceLen=maxWidth - wordsLen
            spaceLen = maxWidth - wordsLen
        }else{
            //单词数量大于等于3时,计算 larCount ,spaceLen
            larCount = (maxWidth - wordsLen) % (wordsCount - 1)
            spaceLen = (maxWidth - wordsLen) / (wordsCount - 1)
        }
        //from ~ to 单词加空格赋值给 resStr
        for idx in from ... to {
            resStr += words[idx]
            var spaceLen1 = spaceLen
            if larCount>0 {
                //若 larCount大于0,索命本次空格数量为 spaceLen + 1
                spaceLen1 += 1
                // larCount - 1
                larCount -= 1
            }
            //若不是最后一个单词,且并非只有一个单词时,添加空额
            //若是最后一个单词,且只有一个单词时,添加空格
            if (idx != to && wordsCount != 1) || (idx == to && wordsCount == 1)  {
                for _ in 0 ..< spaceLen1 {
                    resStr += space
                }
            }
        }
        //返回结果
        return resStr
    }
    func fillLastLine(_ words: [String], maxWidth: Int, from: Int, to: Int, wordsLen: Int) -> String {
        var resStr = ""
        let space = "*"
        //如果 from 小于 to
        if from < to {
            // from ~ to-1 单词和空格 加值到 resStr
            for idx in from ..< to {
                resStr += words[idx] + space
            }
        }
        // resStr 加最后一个单词
        resStr += words[to]
        //剩余空间用空格补齐
        let starNum = maxWidth - resStr.count
        for _ in 0 ..< starNum {
            resStr += space
        }
        //返回结果
        return resStr
    }

代码地址:https://github.com/sinianshou/EGSwiftLearning

SerDes电路的PCB仿真设计规则及策略 SerDes的PCB设计中,重点工作是使用3D 电磁 (EM) 场求解器,进行过孔结构优化,包括 BGA的扇入和扇出区域以及布线的过孔区域,并且,利用连接器供应商提供加密的 3D 模型,进行连接器和 PCB 的级联仿真,最终获取系统级的通道性能参数,如插入损耗、回波损耗、串扰和模式转换、阻抗曲线等,如果发现任何缺陷,再进行进一步优化,这一过程中,推荐进行拆分仿真,以提高设计效率。 阅读详情

相关推荐

别再对着数据手册画图了!用HFSS 2023 R2快速复现ESP-01的PCB天线(附模型文件)

本文详细介绍了使用HFSS 2023 R2逆向复现ESP-01 PCB天线的五个关键步骤,包括逆向工程准备、高效建模技巧、材料与边界设置、激励与求解配置以及结果分析与设计验证。通过参数化建模和智能边界检测,大幅提升仿真效率,适用于WIFI和蓝牙设备的射频设计优化。

weixin_30832143的博客 76

Text Justification

Text Justification 题解 题目描述 即文本对齐:填充单词之间空格使得占满一行(除了最后一行左对齐)。如:words: ["This", "is", "an", "example", "of", "text", "justification."],L: 16.对齐后: "This is an" "examp...

weixin_33851429的博客 179

YOLOv8改进 - 注意力篇 - 引入CAA注意力机制

YOLOv8改进 - 注意力篇 - 引入CAA注意力机制

浪子L的博客 5223

【leetcode】第68题 Text Justification 解析及java代码

这题164赞538踩。。好惨 就是输入一个字符串数组,数组里每一项是一个单词。再输入一个maxWidth,这个长度是输出每一行的字符串长度。 要求是每个单词和每个单词之间最少一个空格,如果一行最后剩余的位置不能再放一个单词,就把这一行后面的空格平均分到前面几个单词中间。 如果这一行只有一个单词或者这是最后一行,那么就不用管后面的空格了(但是还是要补充空格到maxWidth)。 我的思路比较...

722

【学习总结】飞跨电容三电平boost拓扑发波分析与matlab仿真

正常工作时,飞跨电容的电压大约控制在输出电压的一半,降低了开关器件的耐受电压。

晞子的技术札记 1万+

OTP/EEPROM 读取与处理

Camera OTP/EEPROM 读取与处理 文章目录Camera OTP/EEPROM 读取与处理1. OTP概念1.1 基本概念1.2 OTP存储类型1.3 OTP烧录信息类型1.4 OTP 作用2. 配置准备2.1 基本认识2.2 OTP/EEPROM配置3. OTP/EEPROM datasheet理解 以及 读取方式3.1 data sheet 内容3.2 一般OTP/EEPROM(e...

Understand-image 1万+

Text Justification -- LeetCode

原题链接:http://oj.leetcode.com/problems/text-justification/这道题属于纯粹的字符串操作,要把一串单词安排成多行限定长度的字符串。主要难点在于空格的安排,首先每个单词之间必须有空格隔开,而当当前行放不下更多的单词并且字符又不能填满长度L时,我们要把空格均匀的填充在单词之间。如果剩余的空格量刚好是间隔倍数那么就均匀分配即可,否则还必须把多的一个空格放...

编程语言小筑 211

制造业私有化LLM部署实战:从RAG到智能体,构建企业专属知识大脑

大型语言模型(LLM)作为人工智能的核心技术,通过在海量文本数据上进行预训练,具备了强大的语言理解和生成能力。其原理在于基于Transformer架构,通过自注意力机制捕捉长距离语义依赖。这一技术价值在于能将非结构化信息转化为可计算、可推理的知识,从而赋能企业实现知识的高效管理与应用。在制造业等强数据安全与知识密集型领域,私有化部署的LLM结合检索增强生成(RAG)和智能体(Agent)技术,能够深入业务流程,构建安全、专属的智能知识中枢。本文聚焦于如何利用Qwen、LangChain、向量数据库等技术栈,

weixin_42523711的博客 247

Leetcode算法——68、文本对齐(text justification)

给定一个单词序列和一个最大宽度maxWidth,将文本进行格式化,每一行的字符长度为maxWidth。 需要使用贪婪的方法来格式化:在每一行中放入尽可能多的单词。有必要的话,可以添加额外的空格’ ',来保证每行的长度严格等于maxWidth。 单词之间的空格要尽可能均分。如果一行中的空格数量不能被均分,则左边的空位要比右边的空位包含的空格数要多。 特殊规则:文本的最后一行,在单词之间只能出现一个空...

HappyRocking的专栏 1539

Modbus CRC和LRC算法研究及代码实现

一、CRC(循环冗余校验) 1 CRC16实现流程 XOR = 异或 N = 字节的信息位 POLY = CRC16 多项式计算 = 1010 0000 0000 0001 (生成多项式 = 1 + x2 + x15 + x16) 在CRC16中,发送的第一个字节位低字节 2 CRC16实例推导 3 CRC16代码实现 unsigned short CRC16(unsigned char *a...

Hi, Boy 2891

Ubuntu 22.04+/WSL2 中解决 `error: externally-managed-environment` 并成功安装 PyTorch + vLLM 的完整指南

Ubuntu 22.04+/WSL2 因遵循 PEP 668 规范,系统 Python 被标记为“外部管理”,导致直接使用 pip install 安装包时报错 externally-managed-environment。解决方案是创建虚拟环境: 通过 python3 -m venv ~/vllm-env 创建隔离环境 激活环境后安装 PyTorch(CUDA 12.1)和 vLLM 避免使用 sudo pip 或 --break-system-packages,防止破坏系统依赖 关键点: 虚拟环

刘一说的IT专栏 296

68. 文本对齐

要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。你应该使用 “贪心算法” 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。文本的最后一行应为左对齐,且单词之间不插入额外的空格。函数进行了测试,并释放了分配的内存以避免内存泄漏。,重新排版单词,使其成为每行恰好有。个字符,且左右两端对齐的文本。填充,使得每行恰好有。

qq_40844444的博客 467

Unreal5从入门到精通 专栏目录

欢迎来到“UE5从入门到精通”专栏!在这里,我们将带您踏上一段精彩的旅程,深入探索虚幻引擎5(Unreal Engine 5)的无限可能。不论您是游戏开发的新手,还是希望提升技能的开发者,这里都有适合您的内容。基础知识:了解虚幻引擎5的界面、功能和基本操作,为您的学习打下坚实的基础。项目实战:通过实际项目案例,帮助您从理论走向实践,真实体验游戏开发的每个环节。高级技巧:分享各种高级功能和技巧,包括蓝图、C++编程、材质编辑、光照和渲染等,让您能够创作出更具专业水准的作品。

TxNet 2985

文本左右对齐

给定一个单词数组 和一个长度 ,重新排版单词,使其成为每行恰好有 个字符,且左右两端对齐的文本。你应该使用 “贪心算法” 来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 填充,使得每行恰好有 maxWidth 个字符。要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。文本的最后一行应为左对齐,且单词之间不插入额外的空格。注意:示例 1: 输入: words = ["This", "is", "an", "exampl

weixin_68049050的博客 893

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

ET2012新版免狗免安装绿色软件

ET2012新版免狗免安祖昂绿色软件。,新界面,新视觉,

LeetCode 68. Text Justification(文本对齐)

原题网址:https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified

James Pan 1150

SAP B1中文培訓教材_SDK

公司内部培训资料,对开发工具 ,DIAPI, UIAPI,Add-ON有详细解说

Leetcode 68. 文本左右对齐 Text Justification

Leetcode 68. 文本左右对齐 Text Justification 标签: Leetcode 题目地址:https://leetcode-cn.com/problems/text-justification/ 题目描述 给定一个单词数组和一个长度&nbsp;maxWidth,重新排版单词,使其成为每行恰好有&nbsp;maxWidth&nbsp;个字符,且左右两...

CoderWangSon 277

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

【leetcode】68.Text Justification(文本左右对齐)

Click to expand whatever

描绘自己的王国…… 398

LeetCode OJ 之 Text Justification(文本对齐)

题目: Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approac

灯火阑珊处 609

Text Justification 文本对齐 @LeetCode

快被这道题搞死了。。 根据这篇重写了 http://www.cnblogs.com/TenosDoIt/p/3475275.html 分析:这一题需要注意两个点,a、当该行只放一个单词时,空格全部在右边 b、最后一行中单词间只有一个空格,其余空格全部在右边。然后只要贪心选择,在一行中尽量放多的单词。 package Level4; import java.util.ArrayL

4510

Leetcode: Text Justification

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is,

ZkvIA的博客 2805

LeetCode--Text Justification 文本左右对齐

Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that i

wl_ss的博客 344

【LeetCode】Text Justification 解题报告

【题目】 Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approa

跳出温水的青蛙 5797

【LeetCode】 68. Text Justification 文本左右对齐(Hard)(JAVA)

【LeetCode】 68. Text Justification 文本左右对齐(Hard)(JAVA) 题目地址: https://leetcode.com/problems/text-justification/ 题目描述: Given an array of words and a width maxWidth, format the text such that each lin...

qq_16927853的博客 158

[leetcode]68. Text Justification文字对齐

Given an array of words and a widthmaxWidth, format the text such that each line has exactlymaxWidthcharacters and is fully (left and right) justified. You should pack your words in a greedy appro...

weixin_30596343的博客 75

text-justification

【题目描述】Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; tha...

weixin_36909758的博客 319
上一篇: 【算法】Substring with Concatenation of All Words 所有单词链接而成的子串
下一篇: 【算法】Integer to English Words 数字转换单词
无良剑染
博客等级 码龄11年 3粉丝 65原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值