Text Justification [LeetCode]
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]的更多相关文章
- Text Justification leetcode java
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [leetcode]Text Justification @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- [LeetCode] 68. Text Justification 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 【leetcode】Text Justification(hard) ☆
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- winform继承窗体,无法修改父窗体控件问题处理笔记
问题描述: 一个窗体集成父窗体,发现无法直接修改父窗体的控件,比如修改大小等,父窗体控件已经设置为public,如果做成一个dll被引用无此问题 特征: 不禁使父窗体控件,就算新加一个控件也会这样:鼠 ...
- MySQL(六) —— 自定义函数
自定义函数 用户自定义函数(user-defined function, UDF)是一种对MySQL扩展的途径,其用法与内置函数相同. 参数,返回值 创建自定义函数 CREATE FUNCTION f ...
- BeagleBone Black– 智能家居控制系统 LAS - ESP8266 UDP 服务
NodeMCU 的文档里面终于发现,ESP8266 的GPIO 2 确实是 PIN 4,GPIO 0 是 PIN 3. https://github.com/nodemcu/nodemcu-firmw ...
- windows c dll的创建与调用
DLL代码: // TestDynamic.cpp: implementation of the TestDynamic class. // ///////////////////////////// ...
- Ubuntu Install Chrome Brwoser
在ubuntu下安装chrome浏览器,可以直接从官网下载:http://www.google.cn/intl/zh-CN/chrome/browser/thankyou.html?platform= ...
- git学习笔记01-git最基本的工作原理分布式
git学习的网站 http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 --廖雪峰老师 ...
- Nginx基础知识之————什么是 Nginx?
本课时主要给大家讲解什么是 Nginx 和 Nginx 的功能,Nginx 与其他服务器的性能比较和 Nginx 的优点总结的知识,并结合实例让学员深入理解 Nginx 和 Nginx 的功能以及 N ...
- Jquery中css()方法获取边框长度
1. JQuery中可以使用css()方法获取块元素的边框宽度,如下: $("divMode").css("border-left-width");//左边框长 ...
- [转载] 首席工程师揭秘:LinkedIn大数据后台是如何运作的?(一)
本文作者:Jay Kreps,linkedin公司首席工程师:文章来自于他在linkedin上的分享:原文标题:The Log: What every software engineer should ...
- JavaSE复习_9 集合框架复习
△列表迭代器也是不支持在迭代的时候添加元素的,只是列表迭代器自己定义了增删的方法而已.迭代器可以看成实在两个元素之间的指针,每当调用next就跳过一个元素并返回刚刚跳过的元素. △HashTable不 ...