LeetCode 68. Text Justification(文本格式调整)
2018-03-24 19:44
274 查看
题目描述:
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
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:
Return the formatted lines as:[
"This is an",
"example of text",
"justification. "
] Note: Each word is guaranteed not to exceed L in length.
分析:
题意:给定一个字符串数组words,一个限定长度L,我们按照以下规则对字符串数组进行格式调整:①在限定长度范围内L,用至少一个、尽量均匀的空格连接尽多的单词;②某两个单词长度和超过L,将前一个单词单独处理,后面用空格补齐至L。
思路:这是一道模拟题,需要按照题目要求去进行字符串连接。假设字符串数组大小为n,新的字符串连接开始下标start = 0,新的字符串累加长度len = 0,步骤如下:① 先获得所有字符串单词的最长长度maxLength,如果maxLength > L,那么无法进行连接,直接返回原数组;② 对于i∈0→n - 1,找到第一个i,使得字符串连接累加长度len + (i - start) > L(i - start的含义为,start→i共(i - start + 1)个字符串之间至少需要(i-start)个空格连接),那么start→i - 1之间,需要填充delta = L- len个空格,Ⅰ. 如果 i - 1 - start = 0,说明只有一个字符串需要处理,直接在右边加上delta个空格即可;Ⅱ.如果i - 1 - start > 0, 则q = delta / (i - 1 - start),r = delta % (i - 1 - start),其中q个空格均匀分配,r个空格从左往右逐一分配、分完为止;处理完当前连接字符串,start = i,len = 0,继续进入循环,处理后续字符串。
处理完成后,返回结果。
时间复杂度为O(n)。
代码:
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> ans;
int n = words.size();
// Exceptional Case:
if(n == 0){
return ans;
}
// get max word length
int maxLength = 0;
for(string str: words){
int str_len = str.length();
maxLength = max(maxLength, str_len);
}
if(maxLength > maxWidth){
return ans;
}
int len = 0, start = 0;
int delta, q, r;
string res;
for(int i = 0; i <= n - 1; i++){
len += words[i].length();
if(len + i - start > maxWidth){
res = "";
len -= words[i].length();
delta = maxWidth - len;
if(i - 1 - start == 0){
res += (words[i - 1] + string(delta, ' '));
}
else{
q = delta / (i - 1 - start);
r = delta % (i - 1 - start);
for(int j = start; j <= i - 2; j++){
res += words[j];
if(r > 0){
res += string(q + 1, ' ');
r--;
}
else{
res += string(q, ' ');
}
}
res += words[i - 1];
}
ans.push_back(res);
len = 0;
start = i--;
}
}
res = "";
for(int i = start; i <= n - 2; i++){
res += (words[i] + " ");
}
res += (words[n - 1] + string(maxWidth - len - (n - 1 - start), ' '));
ans.push_back(res);
return ans;
}
};
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 L 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.
分析:
题意:给定一个字符串数组words,一个限定长度L,我们按照以下规则对字符串数组进行格式调整:①在限定长度范围内L,用至少一个、尽量均匀的空格连接尽多的单词;②某两个单词长度和超过L,将前一个单词单独处理,后面用空格补齐至L。
思路:这是一道模拟题,需要按照题目要求去进行字符串连接。假设字符串数组大小为n,新的字符串连接开始下标start = 0,新的字符串累加长度len = 0,步骤如下:① 先获得所有字符串单词的最长长度maxLength,如果maxLength > L,那么无法进行连接,直接返回原数组;② 对于i∈0→n - 1,找到第一个i,使得字符串连接累加长度len + (i - start) > L(i - start的含义为,start→i共(i - start + 1)个字符串之间至少需要(i-start)个空格连接),那么start→i - 1之间,需要填充delta = L- len个空格,Ⅰ. 如果 i - 1 - start = 0,说明只有一个字符串需要处理,直接在右边加上delta个空格即可;Ⅱ.如果i - 1 - start > 0, 则q = delta / (i - 1 - start),r = delta % (i - 1 - start),其中q个空格均匀分配,r个空格从左往右逐一分配、分完为止;处理完当前连接字符串,start = i,len = 0,继续进入循环,处理后续字符串。
处理完成后,返回结果。
时间复杂度为O(n)。
代码:
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> ans;
int n = words.size();
// Exceptional Case:
if(n == 0){
return ans;
}
// get max word length
int maxLength = 0;
for(string str: words){
int str_len = str.length();
maxLength = max(maxLength, str_len);
}
if(maxLength > maxWidth){
return ans;
}
int len = 0, start = 0;
int delta, q, r;
string res;
for(int i = 0; i <= n - 1; i++){
len += words[i].length();
if(len + i - start > maxWidth){
res = "";
len -= words[i].length();
delta = maxWidth - len;
if(i - 1 - start == 0){
res += (words[i - 1] + string(delta, ' '));
}
else{
q = delta / (i - 1 - start);
r = delta % (i - 1 - start);
for(int j = start; j <= i - 2; j++){
res += words[j];
if(r > 0){
res += string(q + 1, ' ');
r--;
}
else{
res += string(q, ' ');
}
}
res += words[i - 1];
}
ans.push_back(res);
len = 0;
start = i--;
}
}
res = "";
for(int i = start; i <= n - 2; i++){
res += (words[i] + " ");
}
res += (words[n - 1] + string(maxWidth - len - (n - 1 - start), ' '));
ans.push_back(res);
return ans;
}
};
相关文章推荐
- 调整ListBox控件的行间距及设置文本格式
- 如何调整批注框文本的字体格式
- 如何调整批注框文本的字体格式
- python-13-如何调整字符串中文本的格式?sub函数
- 正则批量替换 如何调整字符串中文本的格式
- python 4-3 如何调整字符串中文本格式re.sub(p,r'\1/\2',str1)/re.sub('(?P<Name>p),'\g<Name>',str1)
- 4-3调整字符串中的文本格式
- Leetcode 68. Text Justification 文本调整 解题报告
- [原创]从RTF格式 的文件中提取文本。
- matlab处理txt文本文件---数据格式要有规律性,否则要用编写特定方式进行读取
- 【NLP】Tika 文本预处理:抽取各种格式文件内容
- pre保持原格式但文本不超出的解决方案
- SQL Server 获取某时间点后修改的函数Function 并以文本格式显示
- 字符串渲染 多种格式的文本 UILabel显示不同的格式
- Java 调整格式日志输出
- java中怎么设置文本格式
- 论文格式调整小技巧——巧用批注省去反复调整参考文献顺序麻烦!
- iPhone开发之显示指定格式的文本自动改变UIlabel高度的自定义分类—— 方便好用
- vs2013调整src文本字体大小
- [LeetCode] Text Justification 文本左右对齐