一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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, pack as many words as you can in each line. Pad extra spaces ’ ’ when necessary so that each line has exactly Lcharacters.

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.

For example,

words: [“This”, “is”, “an”, “example”, “of”, “text”, “justification.”]

L: 16.

Return the formatted lines as:

[

“This is an”,

“example of text”,

“justification. ”

]

Note: Each word is guaranteed not to exceed L in length.

(二)解题

题目大意:按照一定的格式对文本进行对齐。

需要注意以下几点:

1、只有一个单词直接在后面补空格,如“a”,5 输出“a ”

2、最后一组单词不需要对齐,如“a”,“b” 5 输出“a b ”

3、单词与单词之间至少要有一个空格隔开

具体 思路见代码注释:

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> ret;//返回值
        int width = words[0].size();
        int num = 1;
        int last = 0 ;//纪录每一次maxWidth的起始序号
        for(int i = 1 ; i<words.size() ; i++)
        {
            width+=words[i].size();
            num++;
            if(width+num-1>maxWidth)//这里要注意每个单词之间要用空格隔开
            {
                width-=words[i].size();//清除掉最后一个数
                num--;
                string temp;
                if(num==1){//只有一个单词的情况
                    temp+=words[i-1];
                    while(temp.size()!=maxWidth) temp+=" ";//在后面补齐空格
                    ret.push_back(temp);
                }
                else//多个单词,但不是结尾的情况
                {
                    int blankWidth = maxWidth - width;
                    int gap = 0;
                    for(int j = last ; j < i ; j++)
                    {
                        if(j==i-1) gap=0;//最后一个单词后面不加空格
                        else {
                            gap = blankWidth/(num-1);//每一次进来都要算需要增加多少空格
                            gap = blankWidth%(num-1)>0?gap+1:gap;//保证均匀分布
                            blankWidth -=gap;
                        }
                        temp+=words[j];
                        while(gap>0&&gap--) temp+=" ";
                        num--;
                    }
                    ret.push_back(temp);
                }
                //初始化下一个循环
                last=i;
                width = words[i].size();
                num=1;
            }
        }
        if(last<words.size()){//考虑末尾不足maxWidth的情况
            int j = last;
            string temp;
            while(j<words.size()){//先按一个空格添加words
                temp+=words[j++];
                if(temp.size()<maxWidth) temp+=" ";
            }
            while(temp.size()<maxWidth) temp+=" ";//最后不足maxWidth就用空格补足
            ret.push_back(temp);
        }
        return ret;
    }
};

最新文章

  1. linux中终端控制键Ctrl+C,Ctrl+Z,Ctrl+D的使用场合
  2. ThreadLocal原理及其实际应用
  3. extjs store的操作
  4. js 对象 copy 对象
  5. 使用json格式输出
  6. FLEX 特效
  7. C# 第三方DLL,可以实现PDF转图片,支持32位系统、64位系统
  8. 《转》Java 信号量 Semaphore 介绍
  9. Shiro第六篇【验证码、记住我】
  10. MySql Schema 优化
  11. java中级或者高级面试题分享
  12. 查找数组中重复的唯一元素+时间复杂度O(n)+空间复杂度O(1)
  13. java数据结构面试问题—快慢指针问题
  14. 【底层原理】深入理解Cache (上)
  15. Hadoop记录-hdfs转载
  16. A new session could not be created. (Original error: Requested a new session but one was in progress) (WARNING: The server did not provide any stacktrace information)
  17. [js]js中函数传参判断
  18. yolov3源码darknet在vscode下调试
  19. 1.多表查询 =&gt; 转化为一张联合大表 2.可视化工具 3.pymysql模块
  20. 用basicTrendline画一元线性回归直线的置信区间

热门文章

  1. 日常实用css布局技巧汇总
  2. 一个使用 Web Components 的音乐播放器: MelodyPlayer
  3. python笔记四(dict/set/不可变对象)
  4. Appium--入门demo
  5. UE4使用UMG接口操作界面
  6. chrome官方完整安装包
  7. android M Launcher之LauncherModel (二)
  8. 浅析&quot;Sublabel-Accurate Relaxation of Nonconvex Energies&quot; CVPR 2016 Best Paper Honorable Mention
  9. scheme深拷贝和浅拷贝探索
  10. actionbar详解(二)