在一段时间没刷题之后,我发现脑子严重地滞涩了。这题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. python成长之路【第八篇】:异常处理

    一.异常基础 在编程过程中为了增加友好性,在程序出现bug时一般不会将错误信息显示给用户,而是现实一个提示的页面,通俗来说就是不让用户看见大黄页!!! 语法: try: pass except Exc ...

  2. Maven将依赖的所有jar包打成一个jar

    有些特殊情况下,需要将多个jar包打包成一个jar文件.如果使用maven可以加入如下插件: <build> <plugins> <plugin> <arti ...

  3. asp TreeView控件的使用

    相对于之前发过一个TreeView控件的使用方法 本次利用js操作,页面无刷新,性能提高 Css编码可能时我的模板页样式被继承下来,导致页面变乱,不需要的可以去掉 前台 <style> . ...

  4. vscode 与 python 的约会

    安装python 官网(https://www.python.org/downloads/)下载, 安装. (简单略过). 运行python代码 运行python代码的常见方式有三种: 运行pytho ...

  5. nodejs&npm等概念梳理

    nodejs node node版本 npm nvmw\gnvm等多版本管理 CommonJS.AMD.requirejs grunt.gulp package.json .npmrc npm\nod ...

  6. python3 实现简单的信用卡还款,取款转账功能V2

    仅实现还款,取款,转账,信息查询功能 程序结构: atm(函数主执行程序): #Author by Andy #_*_ coding:utf-8 _*_ import os,sys Father_pa ...

  7. Ajax聊天

    结构: index.html <!DOCTYPE html> <html> <head> <title>index.html</title> ...

  8. Python下载进度条

    import dialog,e32,appuifw def ru(x):return x.decode('utf8') def qu(): appuifw.note(ru('已取消')) wait.c ...

  9. ActiveReports中如何在后台导出运行时绑定数据源报表

    ActiveReports支持运行时绑定数据源功能,这种绑定数据源方法使用较为普及,然而很多系统中都需要在后台导出报表文件,所以用户就很困惑,ActiveReports中如何在后台导出运行时绑定数据源 ...

  10. 【Duke-Image】Week_3 Spatial processing

    Chapter_3 Intensity Transsformations and Spatial Filtering 灰度变换与空间滤波 Intensity transformation functi ...