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. R,RJAVA 安装配置 详细版

    准备工作 系统必须已经安装JDK 并配置好了环境变量. 注:安装的jdk  r  以及系统 尽量保持位数一致   1.下载 R https://mirrors.tuna.tsinghua.edu.cn ...

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

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

  3. 【Kafka】搭建和测试等问题

    1.安装启动kafka #跳转到下载目录cd /opt/setup # 下载安装包 wget http://mirror.bit.edu.cn/apache/kafka/0.10.2.0/kafka_ ...

  4. Python正则表达式详解——re库

    一.简介 1.1.相关链接 官方文档: Python2:https://docs.python.org/2/library/re.html Python3:https://docs.python.or ...

  5. python爬虫基础14-selenium大全8/8-常见问题

    Selenium笔记(8)常见的坑 本文集链接:https://www.jianshu.com/nb/25338984 用Xpath查找数据时无法直接获取节点属性 通常在我们使用xpath时,可以使用 ...

  6. GoF23种设计模式之行为型模式之状态模式

    一.概述         定义对象之间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 二.适用性 1.一个对象的行为取决于它的状态,并且它必须在运行时刻 ...

  7. leetcode-8-pointer

    void deleteNode(ListNode* node) { *node = *node->next; }

  8. LeetCode(164)Maximum Gap

    题目 Given an unsorted array, find the maximum difference between the successive elements in its sorte ...

  9. 原生Ajax+springBoot实现用户登录

    思路:用户输入登录信息——信息传到后台——数据库查询——比较查询结果——返回登录信息(成功/失败) html页面代码: <!DOCTYPE html> <html lang=&quo ...

  10. Cinder配置多Ceph后端步骤

    1. 检查cinder当前backend配置 使用cinder service-list,查看cinder-volume服务的Host字段格式. 旧版格式: 新版格式: 旧版中Host字段是cinde ...