LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/
编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int < int的比较,要用计算机的思维。这道题终究还是没有AC,差不多就这样吧。
要养成好的代码习惯,可以一边写代码,一边debug。注意变量命名之类的,常规问题。
Good good study!
#include <iostream>
#include <vector>
#include <string>
using namespace std; class Solution {
public:
vector< string> fullJustify(vector <string> & words, int L) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
int alreadylen = ;
int beginindex = ,endindex = ;
int averagespace = ,firstspace = ;
vector<string > result; string onepiece("");
for(unsigned int index = ;index<words.size();index++)
{
beginindex = endindex = index;
alreadylen = ; while((index<words.size())&&( static_cast<int>(words[index].size()) <= (L-alreadylen)))
{
int wordsindexsize = words[index].size();
alreadylen += words[index].size()+;
index++;
} endindex = index; if(beginindex == endindex-)
{
onepiece.clear();
onepiece = words[beginindex];
if(L -words[beginindex].size()>)
onepiece.append( L-words[beginindex].size(),' ');
}
else if(index != words.size()+ && index!=words.size())
{
onepiece.clear();
averagespace = (L-alreadylen+)/(endindex-beginindex-);
firstspace = averagespace + (L -alreadylen+)%(endindex-beginindex-); for(int tt = beginindex;tt<endindex;tt++)
{
onepiece +=words[tt];
if(tt==beginindex)
onepiece.append(firstspace+,' ' );
else if(tt!= endindex -)
onepiece.append(averagespace+,' ' );
}
}
else
{
onepiece.clear();
int alreadylength = ;
for(int tt = beginindex;tt<endindex;tt++)
{ onepiece += words[tt];
alreadylength += words[tt].size();
if(tt!=words.size()-)
{
onepiece.append(,' ' );
alreadylength++;
}
else
onepiece.append(L-alreadylength,' ' ); } }
index--;
result.push_back(onepiece);
}
return result;
}
}; int main()
{
Solution *mySolution = new Solution(); //words: [This, "is", "an", "example", "of", "text", "justification."]
//L: 16.
vector<string > words; /*words.push_back( "this");
words.push_back( "is");
words.push_back( "an");
words.push_back( "example");
words.push_back( "of");
words.push_back( "text");
words.push_back( "justification."); */
words.push_back( "what");
words.push_back( "must");
words.push_back( "be");
words.push_back( "shall");
words.push_back( "be.");
vector<string > output;
output = mySolution->fullJustify(words,);
for(unsigned int i = ;i<output.size();i++)
cout<<output[i]<<endl; return ;
}
LeetCode OJ——Text Justification的更多相关文章
- 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 文本对齐
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 ...
- Java for LeetCode 068 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文字对齐
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
- [LeetCode] 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 @ Python
原题地址:https://oj.leetcode.com/problems/text-justification/ 题意: Given an array of words and a length L ...
随机推荐
- centos7.4进入单用户模式
centos7.4进入单用户模式 1 - 在启动grub菜单,选择编辑选项启动 2 - 按键盘e键,来进入编辑界面 3 - 找到Linux 16的那一行,将ro改为rw init=/sysroot/b ...
- RSA与AES实现数据加密传输
RSA.AES简介 RSA:非对称加密,需要提前生成两个密钥(一对的),通过其中一个密钥加密后的数据,只有另一个密钥能解密.通常这两个密钥中有一个会暴漏出来,即对外公开的,这个密钥称为“公钥”,反之另 ...
- GoF23种设计模式之行为型模式之观察者模式
一.概述 定义对象之间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.二.适用性1.当一个抽象模型有两个方面,其中一个方面依赖于另一方面的时 ...
- hdu-1231 连续最大子序列(动态规划)
Time limit1000 ms Memory limit32768 kB 给定K个整数的序列{ N1, N2, ..., NK },其任意连续子序列可表示为{ Ni, Ni+1, ..., Nj ...
- 库函数的使用:POJ1488-TEX Quotes(getline()的使用)
TEX Quotes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9385 Description TEX is a type ...
- c++ override 关键字
描述:override保留字表示当前函数重写了基类的虚函数. 目的:1.在函数比较多的情况下可以提示读者某个函数重写了基类虚函数(表示这个虚函数是从基类继承,不是派生类自己定义的):2.强制编译器检查 ...
- 为工程添加上下文菜单(ContextMenu)
上下文菜单大多数情况下都是为ListView中的Item添加的,长按2S左右跳出菜单,添加上下文菜单必须重写活动中的onCreateContextMenu()方法和onContextItemSelec ...
- mac配置启动mongodb
1.新建文件夹,用于存放数据库文件.建议放在自己用户名的文件夹下,不需要sudo会方便很多. 在Users的自己用户名环境下: mkdir [文件夹名] 2.转到mongodb的Bin目录,执行mon ...
- 匈牙利算法 - Luogu 1963 变换序列
P1963 变换序列 题目描述 对于N个整数0,1,-,N-1,一个变换序列T可以将i变成Ti,其中:Ti∈{0,1,-,N-1}且 {Ti}={0,1,-,N-1}. x,y∈{0,1,-,N-1} ...
- HTML5之中国象棋,附带源码!
好久没写随笔了,好怀恋2013年的日子,因为现在不能回到过去了! 再见了 感谢你为我做的一切! 进入正题:HTML5之中国象棋 很小就会下象棋了, 这是象棋的测试地址:点击我吧 然后点击里面的象 ...