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的更多相关文章

  1. leetcode@ [68] Text Justification (String Manipulation)

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

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

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

  3. 【leetcode】Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  4. 【leetcode】Text Justification(hard) ☆

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

  5. 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 ...

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

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

  7. Leetcode#68 Text Justification

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

  8. [LeetCode] Text Justification 文本左右对齐

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

  9. [leetcode]Text Justification @ Python

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

随机推荐

  1. k8s资源指标API及metrics-server资源监控

    简述: 在k8s早期版本中,对资源的监控使用的是heapster的资源监控工具. 但是从 Kubernetes 1.8 开始,Kubernetes 通过 Metrics API 获取资源使用指标,例如 ...

  2. Ubuntu16.04安装后开发工作的配置

    由于多次安装Ubuntu16.04用于学习,其中出了多次问题.每次找参考文件太麻烦,于是写了这篇总结,方便之后备用. 一.精简系统,删除不常用软件 参考资料来自:https://blog.csdn.n ...

  3. selenium+phantomjs爬取京东商品信息

    selenium+phantomjs爬取京东商品信息 今天自己实战写了个爬取京东商品信息,和上一篇的思路一样,附上链接:https://www.cnblogs.com/cany/p/10897618. ...

  4. vscode 实时预览 编辑markdown 插件 Markdown Preview Enhanced

    说明地址: https://shd101wyy.github.io/markdown-preview-enhanced/#/zh-cn/?id=markdown-preview-enhanced

  5. 线段树:POJ3468-A Simple Problem with Integers(线段树注意事项)

    A Simple Problem with Integers Time Limit: 10000MS Memory Limit: 65536K Description You have N integ ...

  6. selenium2等待元素加载

    1.硬性等待 Thread.sleep(8000); 所谓的硬性等待就是,执行完相应操作就等待我设置的8s.无论网速快与慢,网速快的话,也许5s就打开网页了,可是程序必须接着等待剩下的3秒. 网速慢的 ...

  7. HDU 3727 Jewel 主席树

    题意: 一开始有一个空序列,然后有下面四种操作: Insert x在序列尾部加入一个值为\(x\)的元素,而且保证序列中每个元素都互不相同. Query_1 s t k查询区间\([s,t]\)中第\ ...

  8. HDU 5514 Frogs 欧拉函数

    题意: 有\(m(1 \leq m \leq 10^9)\)个石子排成一圈,编号分别为\(0,1,2 \cdots m-1\). 现在在\(0\)号石头上有\(n(1 \leq n \leq 10^4 ...

  9. UVa 1630 区间DP Folding

    一个字符串如果能简写,要么是重复多次,按题中的要求简写:要么是左右两个部分分别简写后再拼起来. dp(i, j)表示字串(i, j)所能被简写的最短的字符串. 判断一个字符串是否为周期串以及求出它的周 ...

  10. No identifier specified for entity: XXXX 错误

    在运行项目的时候报了下面的错误: by: org.hibernate.AnnotationException: No identifier specified for entity: com.exam ...