一天一道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. JavaC命令不能被执行尴尬问题解决

    安装和配置环境变量都按着流程在,但在最后的检验时,发现Java   Java -version 都能运行,唯独Javac 报"不能识别命令"错误信息,下面列出我遇到一个尴尬问题 在 ...

  2. JavaScript中的事件模型

    JS中的事件 1.鼠标事件 onclick   ondbclick   onmouseover   onmouseout 2.HTML事件 onload   onunload   onsubmit   ...

  3. 使用Java可以做得一些事

    安卓 Web JSP使用Echarts的最简单的例子 微信 wechat4j weixin-java-tools weixin4j 网络服务器

  4. Goaccess解析nginx日志备忘

    参考 http://nginx.org/en/docs/http/ngx_http_log_module.html?&_ga=1.92028562.949762386.1481787781#l ...

  5. 安卓 新版本 获取wifi状态网络是否可用等

    写下这篇文章目的:当我学习的和百度看看如何获取网络状态 发现都是一些比较老的方法 API23已结过时 所以在此写下记录 ,我不明白国内为什么那么少 那么我们来看看旧方法 package com.che ...

  6. linux下内存的统计和内存泄露类问题的定位

    在产品的开发中,通过对当前系统消耗内存总量的统计,可以对产品所需内存总量进行精确的评估,从而选择合适的内存芯片与大小,降低产品的成本.在遇到内存泄露类问题时,经常会对此束手无策,本文通过对proc下进 ...

  7. linux下数据同步、回写机制分析

    一.前言在linux2.6.32之前,linux下数据同步是基于pdflush线程机制来实现的,在linux2.6.32以上的版本,内核彻底删掉了pdflush机制,改为了基于per-bdi线程来实现 ...

  8. Objective-C语法概述

    Objective-C语法概述 简称OC 面向对象的C语言 完全兼容C语言 可以在OC里面混入C/C++代码 可以开发IOS和Mac OS X平台应用 语法预览 关键字 基本上都是以@开头(为了与C语 ...

  9. LauncherModel.Callbacks接口

    public interface Callbacks { //如果Launcher在加载完成之前被强制暂停,那么需要通过这个回调方法通知 //launcher,在它再次显示的时候重新执行加载过程 pu ...

  10. [Android]聊聊ActionMode

    最近一段时间都没有更新文章,趁工作之余,更新一篇. 今天介绍一个很常见效果也最容易被忽略的弹出框:ActionMode.主要是ActionMode使用和自己使用过程中遇到的一些问题,相对还是比较简单的 ...