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. NOIp2017囤题计划

    马上就要NOIp2017了,应该囤些题目吧…… 好的这只是一个开始 upd - 11.5 1.p1576 最小花费 无向图,dijisktra 2.p1339 [USACO09OCT]热浪Heat W ...

  2. 【mysql】[Err]1267 - Illegal mix of collations(utf8_general_ci,IMPLICIT) and (utf8_unicode_ci,IMPLICIT) for operation ‘=

    ALTER TABLE table_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;

  3. thinkphp5开发restful-api接口学习 教程视频 接口文档

    目录 1. 获取验证码 2. 用户注册 3. 用户登录 4. 用户上传头像 5. 用户修改密码 6. 用户找回密码 7. 用户绑定手机号 8. 用户绑定邮箱 9. 用户绑定用户名(手机/邮箱) 10. ...

  4. 牛客网暑期ACM多校训练营(第七场)A Minimum Cost Perfect Matching(找规律)

    题意: 给定n, 求一个0~n-1的全排列p, 使得的和最小 分析: 打表发现最优解肯定是和为0的, 然后如果为2的幂就是直接反转即可, 不然的话就要分开从前面到后面逐步拆分, 具体思想模拟一下n = ...

  5. 贪心 - [POI2006]ORK-Ploughing

    [POI2006]ORK-Ploughing 描述 Byteasar 想耕种他那块矩形的田,他每次能耕种矩形的一边(上下左右都行),在他每次耕完后,剩下的田也一定是矩形,每块小区域边长为 1,耕地的长 ...

  6. acdsee 15中文版的许可证密钥+激活方法

    按以下方法就可以使用了,进入注册表的命令是:开始-运行-输入regedit   进入注册表后按下面的步骤操作就行. ACDSee15中文版激活英文版激活码5NR9CW-SSRMMY-KFWMQU-ZP ...

  7. Appscan安全漏洞扫描使用(转)

    这里主要分享如何使用AppScan对一大项目的部分功能进行安全扫描. ----------------------------------------------------------------- ...

  8. python - 路径处理 和 模块导入

    # -*- coding:utf-8 -*- '''@project: jiaxy@author: Jimmy@file: study_模块导入.py@ide: PyCharm Community E ...

  9. 使用SpringMVC参数传递时,解决get请求时中文乱码的问题

    问题描述: 使用SpringMVC参数传递时, 遇到get请求中文信息时,页面应答会显示中文乱码. 解决办法: 一,  我们需要把request.getParameter(“参数名”)获取到的字符串先 ...

  10. PHP-7.1 源代码学习:字节码在 Zend 虚拟机中的解释执行 之 概述

    本文简要介绍 zend 虚拟机解释执行字节码的基本逻辑以及相关的数据结构,关于 PHP 源代码的下载,编译,调试可以参考之前的系列文章 execute_ex 我们来看看执行一个简单的脚本 test.p ...