在一段时间没刷题之后,我发现脑子严重地滞涩了。这题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. Log4j源代码学习

    了解log4j的源代码来源于项目中一次需求,我们想将系统所有的warn日志统一收集到common-warn.log的日志中去,以便于系统对其进行监控处理.于是模拟自动生成的error配置完成了warn ...

  2. HTML中使背景图片自适应浏览器大小

    由于<body>标签的图片不能够拉伸, 解决办法: 1.图片不够大,又background属性不能拉伸图片: 2.只能用个div,把其z-index值设为负,并使这个div大小为整个bod ...

  3. iBatis框架基本使用

    iBatis框架是Java持久层开发框架,说白了就是前人写了一部分代码(针对数据库操作),我们要做的就是再次开发,拿来框架直接使用. 我们自己开发时,dao层的sql语句都是写死在程序中的,如果查询条 ...

  4. JQuery_表单选择器

    表单作为HTML 中一种特殊的元素,操作方法较为多样性和特殊性 开发者不但可以使用之前的常规选择器或过滤器,也可以使用jQuery 为表单专门提供的选择器和过滤器来准确的定位表单元素. 一.常规选择器 ...

  5. [SoapUI] Groovy在SoapUI里获取Text文本第一行数据

    // get external txt file datadef groovyUtils =new com.eviware.soapui.support.GroovyUtils(context)def ...

  6. 关于java序列化中的一个细节

    java序列化机制的可以参考很多资料了,最近在看的时候发现了一些问题. 1. 默认的序列化机制,很多书里讲到序列化类只序列化类名,实例变量,不会实例化类变量(static)和瞬态变量(transien ...

  7. SQLiteDatabase 基本操作

    一.SQLiteOpenHelper类 android 提供类SQLiteOpenHelper帮助用户简化对SQLite数据库的操作.该类是一个抽象类,必须用户自己实现并重写oncreate()和on ...

  8. PHP 文件的操作

    操作文件的步骤: 1.打开文件2.做操作PS!!!3.关闭文件 打开 操作

  9. OpenCV 计算区域的内部参数

    对于一个区域,怎么进一步针对区域内部特征进行处理呢 ? 首先,我们要提取出来内部的某些特征才能说话,下面提取一些简单的特征,话不多说见代码: 1.平均数及方差参数: Mat tempMean, tem ...

  10. 【转】 如何使用Valgrind memcheck工具进行C/C++的内存泄漏检测

    系统编程中一个重要的方面就是有效地处理与内存相关的问题.你的工作越接近系统,你就需要面对越多的内存问题.有时这些问题非常琐碎,而更多时候它会演变成一个调试内存问题的恶梦.所以,在实践中会用到很多工具来 ...