一天一道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;
    }
};

【一天一道LeetCode】#68. Text Justification的更多相关文章

  1. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  2. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  3. [leetcode]68. Text Justification文字对齐

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  4. Leetcode#68 Text Justification

    原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...

  5. 【LeetCode】68. Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  6. 68. Text Justification

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  7. LeetCode OJ——Text Justification

    http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...

  8. 【leetcode】Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  9. 【leetcode】Text Justification(hard) ☆

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

随机推荐

  1. c++自定义类型

    /* --自定义数据类型 结构体 共用体 共用体的数据成员在存储数据时共享存储空间,修改一个成员也会改变另一个成员的值 枚举型 如果要使变量只能使用有限的几个值,则应当使用枚举体.之所以叫枚举体,就是 ...

  2. python学习之路基础篇(第六篇)

    一.算法 冒泡排序 两两比较 打的沉下去,小的浮上来  从而把数字从小到大排列出来 选择排序 随机取一个索引作为最大值,然后和列表中的其他索引进行比较,如果l[0]<l[1],则将l[1]修改为 ...

  3. MongoDB 复制(副本集)

    MongoDB复制是将数据同步在多个服务器的过程. 复制提供了数据的冗余备份,并在多个服务器上存储数据副本,提高了数据的可用性, 并可以保证数据的安全性. 复制还允许您从硬件故障和服务中断中恢复数据. ...

  4. Linux常见目录及命令介绍

    一.Linux中常用的目录介绍:     /        -根目录     /bin    -命令保存目录(普通用户亦可读取的命令)     /boot    -启动目录,启动相关文件     /d ...

  5. 【机器学习】从SVM到SVR

    注:最近在工作中,高频率的接触到了SVM模型,而且还有使用SVM模型做回归的情况,即SVR.另外考虑到自己从第一次知道这个模型到现在也差不多两年时间了,从最开始的腾云驾雾到现在有了一点直观的认识,花费 ...

  6. Android源码解析——Toast

    简介 Toast是一种向用户快速展示少量信息的视图.当它显示时,它会浮在整个应用层的上面,并且不会获取到焦点.它的设计思想是能够向用户展示些信息,但又能尽量不显得唐突.本篇我们来研读一下Toast的源 ...

  7. 自定义View总结2

    自定义控件: 1.组合控件:将系统原生控件组合起来,加上动画效果,形成一种特殊的UI效果 2.纯粹自定义控件:继承自系统的View,自己去实现view效果 优酷菜单: 1.系统原生的旋转和位置动画并没 ...

  8. 初识RecyclerView

    初识RecyclerView 效果图 RecyclerView与ListView对比(官方) RecyclerView 小组件比 ListView 更高级且更具灵活性. 此小组件是一个用于显示庞大数据 ...

  9. Visual studio debug—Process with an Id of 5616 is not running的解决方法

    今天调试的时候,碰到下面的问题 打开项目的csproj文件,拉到最下方找我我图中红框中的部分,删除它即可.

  10. 1.cocos2dx 3.2环境搭建

    1        所需软件 jdk-7u25-windows-i586.exe python-2.7.8.amd64.msi cocos2d-x-3.2.zip apache-ant-1.9.4.zi ...