Text Justification
在一段时间没刷题之后,我发现脑子严重地滞涩了。这题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的更多相关文章
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- 【leetcode】Text Justification(hard) ☆
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- LeetCode(68) Text Justification
题目 Given an array of words and a length L, format the text such that each line has exactly L charact ...
- LeetCode:Text Justification
题目链接 Given an array of words and a length L, format the text such that each line has exactly L chara ...
- 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 ...
- 68. Text Justification
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- leetcode@ [68] Text Justification (String Manipulation)
https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...
- 【一天一道LeetCode】#68. Text Justification
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [Swift]LeetCode68. 文本左右对齐 | Text Justification
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...
随机推荐
- lucene 搜索demo
package com.ljq.utils; import java.io.File; import java.util.ArrayList; import java.util.List; impor ...
- python走起之第九话
协程 协程,又称微线程,纤程.英文名Coroutine.一句话说明什么是线程:协程是一种用户态的轻量级线程. 协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来 ...
- C++ 应用处理异常崩溃(转载)
有时,在我们的程序运行的时候,会遇到一些崩溃问题,尤其是概率出现的时候,如果能捕获其异常,那么对于我们快速定位bug是很有帮助的 这里借用网上搜寻回来的函数,记录一下: 头文件及库: #include ...
- windows apache开启url rewrite
加载Rewrite模块: 在conf目录下httpd.conf中找到 LoadModule rewrite_module modules/mod_rewrite.so 这句,去掉前边的注释符号“#”, ...
- numpy函数白板
numpy.linspace(start, stop, num=50, endpoint=True, retstep=False) start 起始位置 stop 终止位置 num 个数 endpoi ...
- 数据库 MySQL安装图解
MySQL安装图解 一.MYSQL的安装 1.打开下载的mysql安装文件,双击运行mysql-5.5.40-win32.msi. 2.选择安装类型,有"Typical(默认)". ...
- ExtJS Grid导出excel文件
ExtJS Grid导出excel文件, 需下载POI:链接:http://pan.baidu.com/s/1i3lkPhF 密码:rqbg 1.将Grid表格数据连同表格列名传到后台 2.后台导出e ...
- include包含头文件的语句中,双引号和尖括号的区别是什么?
include包含头文件的语句中,双引号和尖括号的区别是什么? #include <> 格式:引用标准库头文件,编译器从标准库目录开始搜索 尖括号表示只在系统默认目录或者括号内的路径查找 ...
- android studio异常关机后出现的问题
使用android studio 时突然卡死. 重启后所有项目都打不开,提示workspace.xml Error:content is not allowed in prolog 打开work ...
- window内容
window parent top location.href location.reload location.replace