https://leetcode.com/problems/text-justification/

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.

Corner Cases:

A line other than the last line might contain only one word. What should you do in this case?
In this case, that line should be left-justified.

class Solution {
public:
string func(int len) {
string res = "";
while(len--) res += " ";
return res;
}
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> res; res.clear(); //Assuming that the length of longest string in the words must be shorter or equal to maxWidth.
int len = ;
int i = , size = words.size();
while(i < size) {
if(len + words[i].length() == maxWidth) {
len = ;
res.push_back(words[i]);
++i;
}
else if(len + words[i].length() < maxWidth) {
len = len + words[i].length();
int left = i;
while(i+ < words.size() && len++words[i+].length() <= maxWidth) {
len += +words[i+].length();
++i;
}
len = ;
int right = i, tot_len = , tot_empty_len, each_empty_len, remain_empty_len; for(int k = left;k <= right; ++k) tot_len += words[k].length(); tot_empty_len = maxWidth - tot_len;
each_empty_len = (left == right)? tot_empty_len: tot_empty_len / (right-left);
remain_empty_len = tot_empty_len - each_empty_len * (right-left); string r = "";
if(left == right) {
string tmp = func(tot_empty_len);
r += words[left] + tmp;
res.push_back(r);
}
else {
string tmp;
if(right <= size - ) {
for(int k = left; k <= right - ; ++k) {
if(remain_empty_len > ) {
tmp = func(each_empty_len+);
remain_empty_len--;
}
else tmp = func(each_empty_len);
r += words[k] + tmp;
}
r += words[right];
res.push_back(r);
}
else {
for(int k = left; k <= right - ; ++k) {
tmp = func();
r += words[k] + tmp;
}
r += words[right];
if(r.length() < maxWidth) r += func(maxWidth - r.length());
res.push_back(r);
}
}
++i;
}
}// end while return res;
}
};

leetcode 68: Text Justification

leetcode@ [68] Text Justification (String Manipulation)的更多相关文章

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

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

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

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

  3. Leetcode#68 Text Justification

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

  4. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  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. Clojure语法学习-循环

    do和块语句 在Scala中,花括号{}括起来的语句构成一个block,它的值就是最后一个语句的值. scala> val a = { | println("a") | 1} ...

  2. kafka.network.SocketServer分析

    当Kafka启动时,会启动这个SocketServer来接收客户端的连接,处理客户端请求,发送响应. 这个类的注释说明了这个socket server的结构 /** * An NIO socket s ...

  3. POJ1260Pearls

    http://poj.org/problem?id=1260 题意 :这个题大概是讲,给你几种等级不同的珠宝,然后告诉你它的数量和价值,等级是升序排列的,且随等级的升高价值也随之升高,但为了防止有的客 ...

  4. POJ2527+多项式除法

    模拟一遍即可. 注意一些特殊情况,见代码. #include<stdio.h> #include<stdlib.h> #include<math.h> #inclu ...

  5. hbase 使用备忘

    hbase是基于hadoop的,所以hbase服务器必须启动hadoop,这点很重要. 当然hbase其实只用到了dadoop的一个组件 1. 启动hadoop-dfs 在主上执行如下命令,可以把主和 ...

  6. codeforces #313 div1 C

    同BZOJ 3782 上学路线 QAQ 还比那个简单一点 把坐标(1,1)-(n,m)平移成(0,0)-(n-1,m-1) 设dp[i]表示从(1,1)出发第一次经过障碍且到达第i个障碍的方案数 首先 ...

  7. linux下添加PATH环境变量

    添加PATH环境变量,第1种方法:[root@lx_web_s1 ~]# export PATH=/usr/local/webserver/mysql/bin:$PATH 再次查看: [root@lx ...

  8. Java web 项目搭建

    Java web 项目搭建 简介 在上一节java web环境搭建中,我们配置了开发java web项目最基本的环境,现在我们将采用Spring MVC+Spring+Hibernate的架构搭建一个 ...

  9. 利用CCProxy管理小型企业的上网行为

    本实验以实例方式,从操作条件.背景.需求.以及具体要求的几个部分进行说明. 1. 操作条件: 装有Windows Server 2003系统,安装了代理服务程序的虚拟机一台 2. 背景: 为了提高员工 ...

  10. xml格式化

    Vim怎么格式化xml,完全不会,vim的缩进也搞不明白