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 exactlyL characters.

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.

思路:

写了一天,用100多行才搞定。大神直接10+行秒杀我..... 先看大神代码。

关键点:

①循环内一次性循环出该行的单词

②string 可以初始化指定的空格数

③对最后一行 和 其他行的区分 通过  i + k >= words.size() 只有空格数不同

vector<string> fullJustify(vector<string> &words, int L) {
vector<string> res;
for(int i = , k, l; i < words.size(); i += k) {
for(k = l = ; i + k < words.size() and l + words[i+k].size() <= L - k; k++) {
l += words[i+k].size();
}
string tmp = words[i];
for(int j = ; j < k - ; j++) {
if(i + k >= words.size()) tmp += " "; //当前是最后一行 空格1个
else tmp += string((L - l) / (k - ) + (j < (L - l) % (k - )), ' '); //不是最后一行 均匀空格 如果有多余的匀给前面
tmp += words[i+j+];
}
tmp += string(L - tmp.size(), ' '); //一行最后面如果还有地方 一定是最后一行的空格
res.push_back(tmp);
}
return res;
}

我的代码,其实思路都差不多的。这道题没什么技巧。但是写得非常繁琐。

vector<string> fullJustify(vector<string> &words, int L) {
vector<string> ans;
int WordsNum = ;
int WordLength = ;
int totalLength = ; if(L == )
{
ans.push_back("");
return ans;
} for(int i = ; i < words.size(); i++)
{
totalLength += words[i].size();
if(WordLength + WordsNum + words[i].size() == L) //恰好放下新的词 这一行确定
{
string LineAns;
for(int j = i - WordsNum; j < i; j++)
{
LineAns += words[j] + " ";
}
LineAns += words[i];
ans.push_back(LineAns); //初始化下一行的数据
WordsNum = ;
WordLength = ;
}
else if(WordLength + WordsNum + words[i].size() > L) //放不下新的词了
{
string LineAns;
string sGap; if(WordsNum == ) //只有1个词 最右边补空格
{
LineAns += words[i - ];
int n = L - words[i - ].size();
while(n > )
{
LineAns += " ";
n--;
}
ans.push_back(LineAns);
}
else
{
int Gap = L - WordLength;
int Res = Gap % (WordsNum - );
int BaseGap = Gap / (WordsNum - ); while(BaseGap > )
{
sGap += " ";
BaseGap--;
}
for(int j = i - WordsNum; j < i - ; j++)
{
LineAns += words[j] + sGap;
if(Res > )
{
LineAns += " ";
Res--;
}
}
LineAns += words[i - ];
ans.push_back(LineAns);
}
//初始化下一行的数据
WordsNum = ;
WordLength = words[i].size();
}
else
{
WordsNum++;
WordLength += words[i].size();
}
} //处理最后一行
string LineAns;
string sGap; if(WordsNum == )
{
if(totalLength == ) //没有内容 输出一行空格
{
int n = L;
while(n > )
{
LineAns += " ";
n--;
}
ans.push_back(LineAns);
}
}
else
{
for(int j = words.size() - WordsNum; j < words.size() - ; j++)
{
LineAns += words[j] + " ";
}
LineAns += words.back();
while(LineAns.size() < L)
{
LineAns += " ";
}
ans.push_back(LineAns);
} return ans;
}

【leetcode】Text Justification(hard) ☆的更多相关文章

  1. 【leetcode】Text Justification

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

  2. 【LeetCode】字符串 string(共112题)

    [3]Longest Substring Without Repeating Characters (2019年1月22日,复习) [5]Longest Palindromic Substring ( ...

  3. 【LeetCode】堆 heap(共31题)

    链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...

  4. 【LeetCode】排序 sort(共20题)

    链接:https://leetcode.com/tag/sort/ [56]Merge Intervals (2019年1月26日,谷歌tag复习) 合并区间 Input: [[1,3],[2,6], ...

  5. 【LeetCode】未分类(tag里面没有)(共题)

    [334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...

  6. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  7. 【LeetCode】栈 stack(共40题)

    [20]Valid Parentheses (2018年11月28日,复习, ko) 给了一个字符串判断是不是合法的括号配对. 题解:直接stack class Solution { public: ...

  8. 【LeetCode】贪心 greedy(共38题)

    [44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...

  9. 【LeetCode】图论 graph(共20题)

    [133]Clone Graph (2019年3月9日,复习) 给定一个图,返回它的深拷贝. 题解:dfs 或者 bfs 都可以 /* // Definition for a Node. class ...

随机推荐

  1. 有感于三个50岁的美国程序员的生活状态与IT职业杂想

    前言 这篇杂记其实是去年也就是 2013年9月30日写的,还上过博客园十日推荐的首页,后来在整理博客分类时七弄八弄误删掉了好多文章,就包括这一篇.今天,2014年9月29日,恰好恰好一年的时候居然在好 ...

  2. [译]Node.js Best Practices - Part 2

    原文: https://blog.risingstack.com/node-js-best-practices-part-2/ 统一风格 在大团队开发JS应用, 创建一个风格指南是很有必要的. 推荐看 ...

  3. 清北暑假模拟day1 艳阳天

    /* 注意P有可能不是质数,不要用欧拉函数那一套,正解可以倍增,就是等比数列和的性质,注意n是否为奇数 */ #include <cstdio> #include <algorith ...

  4. 3_mysql 主从复制

    mysql 主从复制 网易数据库 石勇 提纲 什么是主从复制 主从复制的原理 主从复制的用途 主从复制的搭建 主从复制的问题 什么是主从复制 数据拷贝 准实时 源-主节点:目的-从节点 主从复制的原理 ...

  5. Mysql InnoDB行锁实现方式

    Mysql InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点 ...

  6. springMVC之国际化

    1.工程结构 2.jar包 3.配置文件spring-config.xml,springMVC配置文件 <?xml version="1.0" encoding=" ...

  7. CMD代码页

    不同字符编码在CMD模式下会出现乱码,需要使用 chcp 代码页 命令来更改代码页显示正常. UTF-8  65001 简体中文 936 437          美国 850          多语 ...

  8. web服务器页面错误代码集

    HTTP 1xx-信息提示 这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个1xx响应. 100-继续. 101-切换协议. 2xx-成功 这类状态代码表明服务器成功地接受了 ...

  9. CCF 模拟B 无脑循环+输入输出外挂

    http://115.28.138.223:81/view.page?opid=2#code 代码一有WA点80分 #include<iostream> #include<cstdi ...

  10. Qt5 托盘模仿qq闪烁,弹消息框实现

    在别人代码基础上做的,课设刚好用上了,贴出来分享Qt5.5.1实现. 图片自己找. #ifndef DIALOG_H #define DIALOG_H #include <QDialog> ...