在一段时间没刷题之后,我发现脑子严重地滞涩了。这题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. 一篇讲Java指令重排和内存可见性的好文

    在这里: http://tech.meituan.com/java-memory-reordering.html 指令重排和内存可见性(缓存不一致)是两个不同的问题. volatile关键字太强,即阻 ...

  2. php 判断当前的操作系统的方法

    因为windows 和 linux 下的标识符是不一样的,所以写了个方法来获取当前的标识符. /** * 对操作系统进行判断 * @return string */ static function j ...

  3. windows PHONE 开发-入门程序构筑

    1:页面的扩展名为:.xaml文件类似于ASPX一样可以编写客户端显示内容和后台处理内容 一般的前台页面的形式为: <Page x:Class="MyFirstApp.MainPage ...

  4. Redis教程(二):String数据类型

    一.概述: 字符串类型是Redis中最为基础的数据存储类型,它在Redis中是二进制安全的,这便意味着该类型可以接受任何格式的数据,如JPEG图像数据或Json对象描述信息等.在Redis中字符串类型 ...

  5. ubuntu16 修改apache端口号

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Hannotate SC" } span.s1 { } (1)apach ...

  6. 对象排序,compareTo

    第一个例子 /*为了比较,让自己的类实现Comparable接口,按照自己想要的排序方式重写compareTo *Map只是提供了对键的排序,但是当我们需要对值排序时就的提供我们自己的比较器 这里 只 ...

  7. iOS开发 获取状态栏的点击事件

    首先我们追踪UIStatusBar的触摸事件,需要在AppDelegate里面加入以下代码 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEve ...

  8. OneSQL助力永辉超市大卖特卖

    数据库集群查询达到10w/s,更新操作5k/s,正常! 应用并发连接达到历史高峰4倍,正常! 业务平稳运行,正常! 永辉微店527大促,圆满成功!这标志着平民软件数据库工程师.accenture咨询实 ...

  9. ssh用户登录

    1.搭建环境,连数据库,建包建类 2.Admin,这是表,表必须有主码 package com.chao.db; /** * Admin entity. @author MyEclipse Persi ...

  10. flask笔记1-安装

    1.创建应用的根目录: 2.在根目录下创建独立的虚拟python运行环境,创建完成后当前目录会有一个文件夹,即该独立环境(使用--no-site-pachages参数将不会复制任何系统python环境 ...