面试经典150题——Day24

news/2024/11/28 13:33:20/

文章目录

    • 一、题目
    • 二、题解

一、题目

68. Text Justification

Given an array of strings 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 does 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 because 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 "
]

Constraints:

1 <= words.length <= 300
1 <= words[i].length <= 20
words[i] consists of only English letters and symbols.
1 <= maxWidth <= 100
words[i].length <= maxWidth

题目来源: leetcode

二、题解

class Solution {// blank 返回长度为 n 的由空格组成的字符串string blank(int n) {return string(n, ' ');}// join 返回用 sep 拼接 [left, right) 范围内的 words 组成的字符串string join(vector<string> &words, int left, int right, string sep) {string s = words[left];for (int i = left + 1; i < right; ++i) {s += sep + words[i];}return s;}public:vector<string> fullJustify(vector<string> &words, int maxWidth) {vector<string> ans;int right = 0, n = words.size();while (true) {int left = right; // 当前行的第一个单词在 words 的位置int sumLen = 0; // 统计这一行单词长度之和// 循环确定当前行可以放多少单词,注意单词之间应至少有一个空格while (right < n && sumLen + words[right].length() + right - left <= maxWidth) {sumLen += words[right++].length();}// 当前行是最后一行:单词左对齐,且单词之间应只有一个空格,在行末填充剩余空格if (right == n) {string s = join(words, left, n, " ");ans.emplace_back(s + blank(maxWidth - s.length()));return ans;}int numWords = right - left;int numSpaces = maxWidth - sumLen;// 当前行只有一个单词:该单词左对齐,在行末填充剩余空格if (numWords == 1) {ans.emplace_back(words[left] + blank(numSpaces));continue;}// 当前行不只一个单词int avgSpaces = numSpaces / (numWords - 1);int extraSpaces = numSpaces % (numWords - 1);string s1 = join(words, left, left + extraSpaces + 1, blank(avgSpaces + 1)); // 拼接额外加一个空格的单词string s2 = join(words, left + extraSpaces + 1, right, blank(avgSpaces)); // 拼接其余单词ans.emplace_back(s1 + blank(avgSpaces) + s2);}}
};

http://www.ppmy.cn/news/1183656.html

相关文章

常用字符串函数拓展

文章目录 字符串拓展函数strncpystrncatstrncmpstrstrstrtokstrerrormemcpymemmovememcmpmemset 库函数模拟实现memmoveqsort 我们在学习C语言时已经学习了一些常见的字符串函数&#xff0c;但这还不能满足我们的需求&#xff0c;为此我们拓展了几个常用的字符串函数。 字符串拓…

MySQL实战2

文章目录 主要内容一.回访用户1.准备工作代码如下&#xff08;示例&#xff09;: 2.目标3.实现代码如下&#xff08;示例&#xff09;: 二.如何找到每个人每月消费的最大天数1.准备工作代码如下&#xff08;示例&#xff09;: 2.目标3.实现代码如下&#xff08;示例&#xff09…

[AUTOSAR][诊断管理][ECU][$22] 读取相关的数据

文章目录 一、简介$22服务的实际用途是什么?$22服务的应用场景有哪些呢?$22服务的诊断格式如何?常见DID总结请求实例服务响应负响应NRCNRC优先级二、示例代码uds22_read_data_by_ld.c一、简介 22服务作为诊断服务种的基础服务,可以简单理解为就是一个用于读取ECU数据的外部…

应用案例|基于三维机器视觉的机器人引导电动汽车充电头自动插拔应用方案

Part.1 项目背景 人类对减少温室气体排放、提高能源效率以及减少对化石燃料的依赖&#xff0c;加速了电动汽车的普及&#xff0c;然而&#xff0c;电动汽车的充电依然面临一些挑战。传统的电动汽车充电通常需要人工干预&#xff0c;插入和拔出充电头&#xff0c;这不仅可能导致…

网络滤波器/网络滤波器/脉冲变压器要怎样进行测试,一般要测试哪些参数?

Hqst华强盛导读&#xff1a;网络滤波器/网络滤波器/脉冲变压器要怎样进行测试&#xff0c;一般要测试哪些参数&#xff1f;测试网络滤波器的测试方法和步骤如何&#xff0c;需用到哪些测试工具和仪器设备呢&#xff1f; 一&#xff0c;网络流量的监控和过滤能力测试&am…

uniapp 常见的问题以及解决办法

当开发UniApp时&#xff0c;可能会遇到一些常见问题。以下是一些常见问题及其解决办法&#xff1a; 1. 页面或组件无法正常显示 确保页面或组件的路径和文件名的大小写正确。检查模板代码中是否存在错误或不完整的标签闭合。使用调试工具&#xff08;如Chrome开发者工具&…

正点原子嵌入式linux驱动开发——Linux 串口RS232/485/GPS 驱动

串口是很常用的一个外设&#xff0c;在Linux下通常通过串口和其他设备或传感器进行通信&#xff0c;根据 电平的不同&#xff0c;串口分为TTL和RS232。不管是什么样的接口电平&#xff0c;其驱动程序都是一样的&#xff0c;通过外接RS485这样的芯片就可以将串口转换为RS485信号…

Java之数据类型与变量

目录 1. 字面常量 2. 数据类型 3. 变量 3.1 变量概念 3.2 语法格式 3.3 整型变量 3.3.1 整型变量 3.3.2 长整型变量 3.3.3 短整型变量 3.3.4 字节型变量 3.4 浮点型变量 3.4.1 双精度浮点型 3.4.2 单精度浮点型 3.5 字符型变量 3.6 布尔型变量 3.7 类型转换 3.7…