Problem Description:http://oj.leetcode.com/problems/text-justification/

Note: Just be careful about boundary when implementing it.

 class Solution {
public:
string genNormalLine(vector<string> &words, int L, int start, int end){
int word_count = end - start + ;
string line;
if(word_count == ){
line.append(words[start]);
line.append(L-line.size(), ' ');
}else{
int slot_num = word_count - ;
int words_size = ;
for(int i = start; i <= end; i++)
words_size += words[i].size(); int space_num = L - words_size;
int reminder = space_num % slot_num;
int quotient = space_num / slot_num;
line.append(words[start]);
for(int i = start + ; i <= end; i++) {
if(reminder > ){
line.append(quotient + , ' ');
reminder --;
} else {
line.append(quotient, ' ');
} line.append(words[i]);
}
} return line;
} vector<string> fullJustify(vector<string> &words, int L) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<string> formatted_lines;
if(words.size() == )
return formatted_lines;
for(int i = ; i < words.size();){
int length = ; int j = i;
for(; j < words.size(); j ++){
if( j == i)
length += words[j].size();
else
length += (+ words[j].size()); if(length > L)
break;
} //put words in a line
if(j == words.size()) {
//last line
string line;
line.append(words[i]);
for(int k = i + ; k < words.size(); k++){
line.push_back(' ');
line.append(words[k]);
}
if(line.size() < L){
line.append(L-line.size(), ' ');
} formatted_lines.push_back(line);
}else {
//normal line
formatted_lines.push_back(genNormalLine(words,L, i, j - ));
} //next start index
i = j;
} return formatted_lines;
}
};

Text Justification [LeetCode]的更多相关文章

  1. Text Justification leetcode java

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

  2. [LeetCode] 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 (String Manipulation)

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

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

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

  5. [leetcode]Text Justification @ Python

    原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...

  6. LeetCode OJ——Text Justification

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

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

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

  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. ubuntu下读取数据库中文乱码解决

    请按如下配置myqsl.cnf (/etc/mysql/mysql.conf.d/mysql.cnf ),然后重启mysql服务,对于web程序,你可以把web所有编码都搞成utf-8[client] ...

  2. SOME USEFUL MACHINE LEARNING LIBRARIES.

    from: http://www.erogol.com/broad-view-machine-learning-libraries/ http://www.slideshare.net/Vincenz ...

  3. XSS技巧综合

    这周六会在公司分享经验和技巧,把自己的和网上的一些技巧来综合写一些,方便大家和自己: 普通的XSS,储存,反射,DOM 形成的无外乎就是输出点在html标签之间,html属性之间,成为JS代码,称为C ...

  4. [SAP ABAP开发技术总结]ABAP程序之间数据共享与传递

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. SQL疑难杂症【2】解决SQL订阅过程中找不到已经创建的订阅

    之前有写过一篇博客,主要是图解SQL复制技术:图解SQL 2008数据库复制,当时的测试环境是在我本地同一个服务器上面,所以测试的时候可谓是一帆风顺,最近公司要做一个数据同步的事物,所以再次在不同的服 ...

  6. WebSphere MQ 入门指南

    WebSphere MQ 入门指南这是一篇入门指南.我们从最基本的概念说起: 基础概念 对于MQ,我们需要知道4个名词:队列管理器.队列.消息.通道:对于编程设计人员,通常更关心消息和队列,对于维护管 ...

  7. CSS笔记(十一)CSS3之边框

    参考:http://www.w3school.com.cn/css3/css3_border.asp 圆角边框 <!DOCTYPE html> <html> <head& ...

  8. 常见的PC端和移动端表单组件

    http://files.cnblogs.com/samwu/PC%E7%AB%AF%E5%89%8D%E7%AB%AF%E4%BA%A4%E4%BA%92%E7%BB%84%E4%BB%B6.rar

  9. iOS - UIDevice

    前言 NS_CLASS_AVAILABLE_IOS(2_0) @interface UIDevice : NSObject @available(iOS 2.0, *) public class UI ...

  10. 机器学习十大算法之KNN(K最近邻,k-NearestNeighbor)算法

    机器学习十大算法之KNN算法 前段时间一直在搞tkinter,机器学习荒废了一阵子.如今想重新写一个,发现遇到不少问题,不过最终还是解决了.希望与大家共同进步. 闲话少说,进入正题. KNN算法也称最 ...