`

68 Text Justification——leetcode

阅读更多
68 Text Justification
class Solution {
public:
vector<string> fullJustify(vector<string> &words, int L)
    {
        if(words.empty()){
            return words;
        }
        if(L==0){
            return words;
        }
        const int N = words.size();
        int curLen=0;
        int lastLen=0;
        vector<string> ans;
        vector<string> str;
        for(int i=0;i<N;i++)
        {
            if(str.size()>0)
            {
                curLen ++;
            }
            curLen += words[i].size();

            if(curLen<=L)
            {
                str.push_back(words[i]);
                lastLen=curLen;
            }
            else
            {
                //over
                if(str.size()==1)
                {
                    string   newString = str[0];
                    if(L>str[0].size())
                    {
                        newString += string(L-str[0].size(),' ');
                    }
                    ans.push_back(newString);
                }
                else
                {
                    int tabCount = str.size()-1;
                    int q = (L-lastLen)/tabCount;
                    int r = (L-lastLen) - q*tabCount;
                    string   newString ;
                    int j=0;

                    for(;j<tabCount;j++)
                    {
                        newString += str[j];
                        if(j<r){
                            string spaces(q+2,' ');
                            newString += spaces;
                        }
                        else {
                            string spaces(q+1,' ');
                            newString += spaces;
                        }
                    }
                    newString += str[j];
                    ans.push_back(newString);
                }
                str.clear();
                str.push_back(words[i]);
                lastLen = curLen = words[i].size();
            }
        }

        if(str.size()==1)
        {
            string   newString = str[0];
            if(L>str[0].size())
            {
                newString += string(L-str[0].size(),' ');
            }
            ans.push_back(newString);
        }
        else
        {
            int tabCount = str.size()-1;
            string   newString ;
            int j=0;
            int len=0;
            for(;j<tabCount;j++)
            {
                newString += str[j];
                newString +=" ";
                len+=str[j].size()+1;
            }
            newString += str[j];
            len += str[j].size();
            newString += string(L-len,' ');
            ans.push_back(newString);
        }
        return ans;
    }
};

 

这题,其实没啥可说的,需要的是细心,这个是常见的阅读器排版问题。嗯,错过很多次,慢慢修改就对了。

分享到:
评论

相关推荐

    LeetCode最全代码

    # [LeetCode](https://leetcode.com/problemset/algorithms/) ![Language](https://img.shields.io/badge/language-Python%20%2F%20C++%2011-orange.svg) [![License]...

    LaTeX Beginner's Guide——Stefan Kottwitz

    4.Apply intelligent justification and customized hyphenation to achieve fine text design 5.Typeset professional-looking tables and create bulleted and enumerated lists 6.Write sophisticated math ...

    python 实现文本左右对齐

    # 给定一个单词数组和一个长度 maxWidth...# words = ["This", "is", "an", "example", "of", "text", "justification."] # maxWidth = 16 # 输出: # [ # "This is an", # "example of text", # "justification. " # ]

    电子商务英文课件:ch14 Economics and Justification of.ppt

    电子商务英文课件:ch14 Economics and Justification of.ppt

    Agg的.NET移植Agg-Sharp.zip

    AddChild(new TextWidget("Hello World", 320, 240, justification: Font.Justification.Center)); ShowAsSystemWindow(); } // and just for fun lets also draw a circle public ...

    cpp-算法精粹

    Text Justification Max Points on a Line Community QQ 群: 237669375 Github: https://www.github.com/soulmachine/algorithm-essentials 微博: @灵魂机器 License Book License: CC BY-SA 3.0 License

    《天线手册》【美国海军版】.PDF

    全英文,To Our Readers Changes: Readers of this publication are encouraged to submit suggestions and changes that will improve it. Recommendations ...• Justification and/or source of change

    Use TOC and ROI for justification

    这个文档讲述了如何通过TOC和ROI来判断是否进行投资,同样可以通过这一点来说服企业管理层

    Android代码-TextJustify-Android

    Android Full Justification About This library will provide you a way to justify text. It supports both plain text and Spannables. Additionally, the library can auto-hyphentate your displayed content ...

    Fundamental Networking in Java

    " 1.4 provide further justification for the appearance of this text. Much of the information in this book is either absent from or incorrectly specified in the Java documentation and books by other ...

    机器学习经典制作(英语)

    for the discussion and justification of algorithms. It also describes several key aspects of the application of these algorithms. We have aimed to present the most novel theoretical tools and concepts...

    摩托罗拉XTS3000写频软件

    Text Justification :用于选择文本的对齐方式(左对齐或右对齐) 。 Channel Text Size :信道文本长度(与区域文本共享 14 位字符,设置得长,区域文本长度就要缩减。 ) Zone Text Size :区域文本...

    VB编程资源大全(英文源码 其它)

    &lt;END&gt;&lt;br&gt;66,MessageBoxDll.zip This is a custom message box creator.It provides the following functionalities that are not provided by the MsgBox function in VB.&lt;END&gt;&lt;br&gt;68,DragDrop.zip This is ...

    hyphenation-justification-vf

    换行,对齐和可变字体 看一下换行和对齐方式,以及可变字体如何改善对齐文本。 在2018年Robothon大会上演示了使用可变字体改善对齐方式的演示。 如何使用 带上自己的可变字体。 :)代码引用了Gimlet可变字体,但是...

    Pattern-Oriented+Software+Architecture+(Vol.5).pdf

    justification. But design: what happens when we design? Is design about problems or about beauty? Does resolving forces while solving a problem force beauty into the design? Or can beauty and ideal ...

    光网络通信协议-g.709

    17.6 Mapping of other constant bit-rate signals with justification into OPUk 80 17.7 Mapping a 1000BASE-X and FC-1200 signal via timing transparent transcoding into OPUk 80 17.7.1 Mapping a 1000BASE-X...

    光传送网通信协议-G.709

    17.6 Mapping of other constant bit-rate signals with justification into OPUk 80 17.7 Mapping a 1000BASE-X and FC-1200 signal via timing transparent transcoding into OPUk 80 17.7.1 Mapping a 1000BASE-X...

    CS229 Supplemental Lecture notes John Duchi

    is somewhat different from the original interpretation and justification of Freund and Schapire, but which lends itself to our approach of (1) choose a representation, (2) choose a loss, and (3) ...

    2022美赛备赛之论文写作.pdf

    目录 一、Summary 二、introduction Problem Background Problem restatememt Literature review Our work ...三、假设Assumptions and Justification ...三、假设Assumptions and Justification

Global site tag (gtag.js) - Google Analytics