实现office word中的两端对齐功能。

只有个单词时,右边补齐空格。最后一行每个词间一个空格,整下的空格右边补齐。给定字符串,和每行的字符数L。进行两端对齐输出。

我的思路是写一个函数,给定相应的参数就返回该行的string。然后在主函数里只要负责给参数就好了。参数包括words字符串数组本身,然后每个字符串的长度数组。开始start记录从字符串数组只的那个词开始,end记录到哪个词结束。L就是每行的最大字符数。在子函数里实现如果是最后一个单词了,那么就每个词空一个空格。其余后面补齐。在主函数里通过记录start到目前的长度是否等于或者超过L来判断start和end。如果end超过了长度就结束,返回结果即可。代码有点复杂。将近100行。调了9次bug终究AC了。

class Solution {
public:
// 给定相应的参数,返回该行字符串满足右对齐
string fun68(vector<string> &words, int len[], int start, int end, int L)
{
if(start == end)
{
string subans = words[start];
for (int i = ; i < L - len[start]; ++i)
subans += ' ';
return subans;
}
string ans = "";
int tolen = , inter = ; // 词的长度tolen,两个词之间的空格inter
for (int i = start; i <= end; i++)
{
tolen += len[i];
}
int left = L - tolen;
inter = left / (end - start);
int times = left - inter * (end - start);//剩times个空格需要在前times个词间加一个空格
if(end == words.size() -) // 如果是最后一个了,那么每个单词空一格单词,剩下的空格放在最后边
{
for (int i = start; i < words.size() - ; i++)
{
ans += words[i] + ' ';
}
ans += words[end];
left -= (end - start);
for (int i = ; i < left; i++)
ans += ' ';
}
else // 否则就是平均分空格
{
for (int i = start; i <start + times; ++i)
{
ans += words[i];
for (int j = ; j < inter + ; ++j)
{
ans += " ";
}
}
for (int i = start + times; i < end; ++i) // 多出来的不能平均分的空格从左边开始分
{
ans += words[i];
for (int j = ; j < inter; ++j)
{
ans += " ";
}
}
ans += words[end];
}
return ans;
}
//主函数
vector<string> fullJustify(vector<string> &words, int L)
{
vector<string> ans;
if (words.size() == )
{
return ans;
}
int n = words.size();
int *len = new int[n];
for (int i = ; i < n; ++i)
{
len[i] = words[i].size();
}
int sum1 = len[], sum2 = , start = , end = ; // sum1记录从start开始的词到end的包括空格的长度
string tmp = "";
while ()
{
if (sum1 == L || sum1 + == L) // 如果有满足刚好等于L,那么就是要输出一行了
{
ans.push_back(fun68(words, len, start, end, L));
start = end + ;
end = end + ;
if(end < n)// 没输出一行要相应的该sum的值,相当于重新计算过
{
sum1 = len[end];
sum2 = ;
continue;
}
else
break;
}
else if (end + < n)
sum2 = sum1 + + len[end + ];
else
{
ans.push_back(fun68(words, len, start, end, L));
break;
} if (sum1 < L && sum2 > L) // 说明到end应结束一行
{
ans.push_back(fun68(words, len, start, end, L));
start = end + ;
end = start;
sum1 = len[end];
sum2 = ;
continue;
}
sum1 += + len[end + ];
end++;
}
return ans;
}
};

原创,但不是最好的。有时间了再学习学习50行的。

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

  2. Text Justification 文本左右对齐

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

  3. 实现css两端对齐

    如何实现css的两端对齐功能? 最近做项目遇到这种情况,如图所示: input左边框的用户,旧密码,新密码,确认密码无法对齐,样式很丑. 解决办法: 找到对应的类名,加上:text-align:jus ...

  4. 68. Text Justification一行单词 两端对齐

    [抄题]: Given an array of words and a width maxWidth, format the text such that each line has exactly  ...

  5. [MIT6.006] 20. Daynamic Programming II: Text Justification, Blackjack 动态规划II:文本对齐,黑杰克

    这节课通过讲解动态规划在文本对齐(Text Justification)和黑杰克(Blackjack)上的求解过程,来帮助我们理解动态规划的通用求解的五个步骤: 动态规划求解的五个"简单&q ...

  6. [Swift]LeetCode68. 文本左右对齐 | Text Justification

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

  7. Text Justification,文本对齐

    问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐. words: ["This", ...

  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] 68. Text Justification 文本对齐

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

随机推荐

  1. EEPlat的元数据驱动的运行引擎

    EEPlat採用了元数据驱动的核心思想,因而EEPlat最重要的就是完好的元模型体系及高效灵活的解析运行引擎.EEPlat的运行引擎通过解析基于元模型的元数据,解释运行形成终于的业务系统. EEPla ...

  2. C++ 静态static 变量在 cocos2d-x 里面使用误区

    void Cms::showMonster(CCArray* monsterArray,int type) { <span style="color:#ff0000;"> ...

  3. libsvm工具箱C++编程实践2

    转载请注明出处  http://blog.csdn.net/u013491262/article/details/37344193   点击打开链接 上周因为皮肤有点过敏,去医院来来回回一周. 前几天 ...

  4. ssis 到别的表查找临时变量值

    原文:ssis 到别的表查找临时变量值 etl过程过,往一个数据库表插入数据,插入的值往往需要到另外一个数据库读取.例如下面的客户跟踪,需要一个"项目ID",这个ID需要到另一个数 ...

  5. opencv-阈值处理

    从原理:http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/imgproc/threshold/threshold.html 目标: ...

  6. leetcode第一刷_Length of Last Word

    不是非常明确出题人的意图,事实上这道题用java的话简直是太简单了,用split处理一下,得到全部单词的一个数组,然后求最后一个的长度即可了.我个人认为java里最成功的函数就是split了,我做pr ...

  7. SVN和Git的一些用法总结(转)

    转载请注明出处:http://www.codelast.com/ 以下都是比较基础的操作,高手们请绕道,不必浪费时间来看了. (A)SVN (1)查看日志提交的时候一般会写上注释,如果要查看提交日志, ...

  8. 使用 CXF 做 webservice 简单例子[转]

    Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构.它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量 ...

  9. 第22章 职责链模式(Chain of Responsibility)

    原文 第22章 职责链模式(Chain of Responsibility) 职责链模式 导读:职责链模式是一个既简单又复杂的设计模式,刚开始学习这个设计模式的时候光示例都看了好几遍.就为了理清里面的 ...

  10. windows下系统移植到linux下出现的问题

    今天遇到了一个之前没有遇到的问题,记录一下. 我们是在windows下进行开发的,最终系统是部署在linux服务器上. 在windows一切正常,但是部署到linux下时,有些功能不能用了.通过log ...