C++实现LeetCode(68.文本左右对齐)

 更新时间:2021年07月17日 08:40:58   作者:Grandyang  
这篇文章主要介绍了C++实现LeetCode(68.文本左右对齐),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

[LeetCode] 68.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 maxWidthcharacters.

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 extraspace 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                  "
]

我将这道题翻译为文本的左右对齐是因为这道题像极了word软件里面的文本左右对齐功能,这道题我前前后后折腾了快四个小时终于通过了OJ,完成了之后想着去网上搜搜看有没有更简单的方法,搜了一圈发现都差不多,都挺复杂的,于是乎就按自己的思路来说吧,由于返回的结果是多行的,所以我们在处理的时候也要一行一行的来处理,首先要做的就是确定每一行能放下的单词数,这个不难,就是比较n个单词的长度和加上n - 1个空格的长度跟给定的长度L来比较即可,找到了一行能放下的单词个数,然后计算出这一行存在的空格的个数,是用给定的长度L减去这一行所有单词的长度和。得到了空格的个数之后,就要在每个单词后面插入这些空格,这里有两种情况,比如某一行有两个单词"to" 和 "a",给定长度L为6,如果这行不是最后一行,那么应该输出"to   a",如果是最后一行,则应该输出 "to a  ",所以这里需要分情况讨论,最后一行的处理方法和其他行之间略有不同。最后一个难点就是,如果一行有三个单词,这时候中间有两个空,如果空格数不是2的倍数,那么左边的空间里要比右边的空间里多加入一个空格,那么我们只需要用总的空格数除以空间个数,能除尽最好,说明能平均分配,除不尽的话就多加个空格放在左边的空间里,以此类推,具体实现过程还是看代码吧:

class Solution {
public:
    vector<string> fullJustify(vector<string> &words, int L) {
        vector<string> res;
        int i = 0;
        while (i < words.size()) {
            int j = i, len = 0;
            while (j < words.size() && len + words[j].size() + j - i <= L) {
                len += words[j++].size();
            }
            string out;
            int space = L - len;
            for (int k = i; k < j; ++k) {
                out += words[k];
                if (space > 0) {
                    int tmp;
                    if (j == words.size()) { 
                        if (j - k == 1) tmp = space;
                        else tmp = 1;
                    } else {
                        if (j - k - 1 > 0) {
                            if (space % (j - k - 1) == 0) tmp = space / (j - k - 1);
                            else tmp = space / (j - k - 1) + 1;
                        } else tmp = space;
                    }
                    out.append(tmp, ' ');
                    space -= tmp;
                }
            }
            res.push_back(out);
            i = j;
        }
        return res;
    }
};

到此这篇关于C++实现LeetCode(68.文本左右对齐)的文章就介绍到这了,更多相关C++实现文本左右对齐内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • openCV实现图像分割

    openCV实现图像分割

    这篇文章主要为大家详细介绍了openCV实现图像分割,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-09-09
  • C++ using 编译指令与名称冲突问题

    C++ using 编译指令与名称冲突问题

    using 编译指令由名称空间名和它前面的关键字 using namespace 组成,它使名称空间中的所有名称都可用,而不需要使用作用域解析运算符,这篇文章主要介绍了C++ using 编译指令与名称冲突,需要的朋友可以参考下
    2022-11-11
  • C++ com编程学习详解

    C++ com编程学习详解

    这篇文章主要介绍了C++ COM编程的学习过程,在C++中,可以使用抽象基类来实现COM接口,需要的朋友可以参考下,希望能够给你带来帮助
    2021-09-09
  • Linux C 时间函数应用

    Linux C 时间函数应用

    本文是关于Linux C时间函数 time_t struct tm 进行了详细的分析介绍并有应用实例,希望能帮到有需要的同学
    2016-07-07
  • C++实现ETW进行进程变动监控详解

    C++实现ETW进行进程变动监控详解

    ETW提供了一种对用户层应用程序和内核层驱动创建的事件对象的跟踪记录机制。为开发者提供了一套快速、可靠、通用的一系列事件跟踪特性。本文将利用ETW进行进程变动监控,需要的可以参考一下
    2022-07-07
  • C++ 创建桌面快捷方式 开始菜单的实现代码

    C++ 创建桌面快捷方式 开始菜单的实现代码

    这篇文章介绍了C++ 创建桌面快捷方式,开始菜单的实现代码,需要的朋友可以参考一下
    2013-06-06
  • 使用Qt实现监听网页是否响应并导出Excel表

    使用Qt实现监听网页是否响应并导出Excel表

    Qt导出数据到excel,方法有很多,下面这篇文章主要给大家介绍了关于使用Qt实现监听网页是否响应并导出Excel表的相关资料,文中通过代码示例介绍的非常详细,需要的朋友可以参考下
    2023-11-11
  • C++遗传算法类文件实例分析

    C++遗传算法类文件实例分析

    这篇文章主要介绍了C++遗传算法的一个类文件,是学习遗传算法的绝佳参考资料,需要的朋友可以参考下
    2014-08-08
  • opencv求解区域的内接矩形

    opencv求解区域的内接矩形

    这篇文章主要为大家详细介绍了opencv求解区域的内接矩形,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-07-07
  • C语言控制台绘制曲线的实现代码

    C语言控制台绘制曲线的实现代码

    这篇文章主要为大家详细介绍了C语言控制台绘制曲线的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-06-06

最新评论