欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

算法:文本左右对齐

发布时间:2025/6/15 编程问答 28 豆豆
生活随笔 收集整理的这篇文章主要介绍了 算法:文本左右对齐 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

题目

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

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

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

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

说明:

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

示例:

输入: words = ["This", "is", "an", "example", "of", "text", "justification."] maxWidth = 16 输出: ["This is an","example of text","justification. " ] //文本左右对齐1.尽可能多的在每行放置更多的单词,必要时用' '填充 2.要求更可能均匀的分配单词间空格的数量,如果某一行不能均匀分配,那左侧放置的空格数要多于右侧 3.文本最后一行要左对齐,且单词之间不能插入额外的空格string fillWords(vector<string>& words, int bg, int ed, int maxWidth, bool lastLine = false){//单词数量int wordCount = ed - bg + 1;int spaceCount = maxWidth + 1 - wordCount; // 除去每个单词尾部空格, + 1 是最后一个单词的尾部空格的特殊处理for (int i = bg; i <= ed; i++){spaceCount -= words[i].size(); // 除去所有单词的长度}//spaceCount是剩下的总空格数int spaceSuffix = 1; // 词尾空格int spaceAvg = (wordCount == 1) ? 1 : spaceCount / (wordCount - 1); // 额外空格的平均值int spaceExtra = (wordCount == 1) ? 0 : spaceCount % (wordCount - 1); // 额外空格的余数string ans;for (int i = bg; i < ed; i++){ans += words[i]; // 填入单词if (lastLine) // 特殊处理最后一行{fill_n(back_inserter(ans), 1, ' ');continue;}fill_n(back_inserter(ans), spaceSuffix + spaceAvg + ((i - bg) < spaceExtra), ' '); // 根据计算结果补上空格}ans += words[ed]; // 填入最后一个单词fill_n(back_inserter(ans), maxWidth - ans.size(), ' '); // 补上这一行最后的空格return ans; }//每行有maxWidth字符,包括空格 vector<string> fullJustify(vector<string>& words, int maxWidth) {vector<string> ans;int cnt = 0;int bg = 0;for (int i = 0; i < words.size(); i++){cnt += words[i].size() + 1;if (i + 1 == words.size() || cnt + words[i + 1].size() > maxWidth){ // 如果是最后一个单词,或者加上下一个词就超过长度了,即可凑成一行ans.push_back(fillWords(words, bg, i, maxWidth, i + 1 == words.size()));bg = i + 1;cnt = 0;}}return ans; }

链接:https://leetcode-cn.com/problems/text-justification/solution/text-justification-by-ikaruga/

总结

以上是生活随笔为你收集整理的算法:文本左右对齐的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。