在一段时间没刷题之后,我发现脑子严重地滞涩了。这题AC花了好大力气,出现了Memory Limit Exceed,Core Dump,以及各种普通的编译、运行错误。MLE 和 CD错误是比较难检查的, 只能通过打印来定位,最后发现原来是循环变量未赋值,导致死循环!!!这种错误简直是不能原谅的。二至于其他的各种问题,则是由于脑子里面没有清晰的算法思路,以致一些case不能通过。这种问题,需要经过大量的训练,以及集中精力地编写代码,才能逐渐减少甚至避免。

  当然,另一方面,总体思路上,比第一次AC清晰了许多。每一行的形成需要在下一行开头单词出现以后进行判断以及添加,这样避免了在当前行进行判断需要考虑多种情况的问题。代码如下:

public class Solution {
public ArrayList<String> fullJustify(String[] words, int L)
{
ArrayList<String> res=new ArrayList<String>();
ArrayList<String> line=new ArrayList<String>();
int len=;
for(int i=;i<words.length;i++)
{
String cw=words[i];
if(line.size()+len+cw.length()>L)
{
String s="";
String interval="";
int sc=,sr=;
if(line.size()==)
{
sc=L-len;
sr=;
s=line.get();
for(int j=;j<sc;j++)
interval+=" ";
s+=interval;
res.add(s);
}
else
{
sc=(L-len)/(line.size()-);
sr=(L-len)%(line.size()-);
s=s+line.get();
for(int j=;j<sc;j++)
interval+=" ";
int ct=sr;
for(int j=;j<line.size();j++)
{
String ts=interval;
if(ct>)
{
ts=interval+" ";
ct--;
}
s=s+ts;
s=s+line.get(j);
}
res.add(s); }
len=cw.length();
line=new ArrayList<String>();
line.add(cw);
if(i==words.length-)
{
s=cw;
sc=L-cw.length();
for(int j=;j<sc;j++)
s=s+" ";
res.add(s);
}
}
else
{
line.add(cw);
len+=cw.length();
int l=;
if(i==words.length-)
{
String s=line.get();
l=s.length();
for(int j=;j<line.size();j++)
{
s=s+" ";
s=s+line.get(j);
l=l++line.get(j).length();
}
int rem=L-l;
for(int j=;j<rem;j++)
{
s=s+" ";
}
res.add(s);
}
}
}
return res;
}
}

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. 【leetcode】Text Justification

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

  3. 【leetcode】Text Justification(hard) ☆

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

  4. LeetCode(68) Text Justification

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

  5. LeetCode:Text Justification

    题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...

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

  7. 68. Text Justification

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

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

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

  9. 【一天一道LeetCode】#68. Text Justification

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

随机推荐

  1. Mac上配置Privoxy

    此文档适用于走Shadowsocks代理,想利用Privoxy将主机作为代理服务器的用户. 0.安装完Privoxy后,打开终端命令. 1.打开Privoxy的配置文件config: cd /usr/ ...

  2. kafka使用getOffsetsBefore()获取获取offset异常分析

    根据时间戳获取kafka的topic的偏移量,结果获取的偏移量量数据组的长度为0,就会出现如下的数组下标越界的异常,实现的原理是使用了kafka的getOffsetsBefore()方法: Excep ...

  3. Android自定义View的三种实现方式

    在毕设项目中多处用到自定义控件,一直打算总结一下自定义控件的实现方式,今天就来总结一下吧.在此之前学习了郭霖大神博客上面关于自定义View的几篇博文,感觉受益良多,本文中就参考了其中的一些内容. 总结 ...

  4. BZOJ2933:POI1999地图

    Description     一个人口统计办公室要绘制一张地图.由于技术的原因只能使用少量的颜色.两个有相同或相近人口的区域在地图应用相同的颜色.例如一种颜色k,则A(k) 是相应的数,则有: 在用 ...

  5. Android之Dialer之紧急号码

    Android之Dialer之紧急号码 e over any other (e.g. supplementary service related) number analysis. a) 112 an ...

  6. unity平台的预处理

    unity平台的预处理     在开发中,特别是unity的跨平台中,我们经常会在各个平台游走,如安卓版,苹果版,PC版.......在此不同的平台上,有可能我们需要做不同的操作.然而我们就可以用un ...

  7. CSS3图片倒影技术实现及原理

    CSS3图片倒影技术实现及原理 目前为止我们已经探讨了很多CSS3中的新功能和新特征.除了上面这些,实际上还有很多CSS新属性并未包含进CSS3官方标准中,像谷歌浏览器或火狐浏览器等都会利用CSS的浏 ...

  8. 使用scp命令下载服务器文件

    scp -P 7022 admin@ip:/newnow/mysql/backup.sql 7birds.sql -P 指定端口号,默认为22

  9. 【转】Expire Google Drive Files 让Google Docs云盘共享连接在指定时间后自动失效

    最近在清理Google Docs中之前共享过的文件链接,发现Google Docs多人协作共享过的链接会一直存在,在实际操作中较不灵活.正好订阅的RSS推送了Pseric写的这篇文章 - Expire ...

  10. 对Android开发者有益的40条优化建议

    下面是开始Android编程的好方法: 找一些与你想做事情类似的代码 调整它,尝试让它做你像做的事情 经历问题 使用StackOverflow解决问题 对每个你像添加的特征重复上述过程.这种方法能够激 ...