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 and is fully (left and right) justified.
You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces ' '
when necessary so that each line has exactly L characters.
Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.
For the last line of text, it should be left justified and no extra space is inserted between words.
For example,
words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16
.
解题思路:
直接按照约束条件老实实现即可,JAVA实现如下:
static public List<String> fullJustify(String[] words, int maxWidth) {
List<String> list=new ArrayList<String>();
int start=0,end=-1,sum=0;
for(int i=0;i<words.length;i++){
sum+=words[i].length();
if(sum>maxWidth){
i--;
start=end+1;
end=i;
sum=0;
list.add(oneString(words,start,end,maxWidth));
continue;
}
sum++;
}
if(sum>0)
list.add(oneString(words,end+1,words.length-1,maxWidth));
return list;
}
static public String oneString(String[] words,int start,int end,int maxWidth){
StringBuilder sb=new StringBuilder();
if(start==end)
sb.append(words[start]);
else if(end==words.length-1){
for(int i=start;i<=end-1;i++)
sb.append(words[i]+" ");
sb.append(words[end]);
}
else{
int spaceSum=maxWidth;
for(int i=start;i<=end;i++)
spaceSum-=words[i].length();
int extra=spaceSum-(spaceSum/(end-start))*(end-start);
for(int i=start;i<=end;i++){
sb.append(words[i]);
for(int j=0;j<spaceSum/(end-start)&&i!=end;j++)
sb.append(" ");
if(extra-->0)
sb.append(" ");
}
}
while(sb.length()<maxWidth)
sb.append(" ");
return sb.toString();
}
Java for LeetCode 068 Text Justification的更多相关文章
- 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 文本对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
- LeetCode OJ——Text Justification
http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...
- 【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 width maxWidth, format the text such that each line has exactly maxWid ...
- Leetcode#68 Text Justification
原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...
- Text Justification leetcode java
题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...
- [LeetCode] Text Justification 文本左右对齐
Given an array of words and a length L, format the text such that each line has exactly L characters ...
随机推荐
- Redis Installation、Configuration、Program Based On Redis Learning
目录 . Redis 简介 . Redis安装配置 . 编程使用Redis . 使用Lua脚本 1. Redis 简介 0x1: Redis是什么 Redis是一款Nosql类型的基于key-valu ...
- Nagios安装部署和介绍(一)
一.软件版本下载 Nagios版本下载地址: http://prdownloads.sourceforge.net/sourceforge/nagios/ http://sourceforge.net ...
- js获取页面url的方法
我们可以用javascript获得其中的各个部分 1, window.location.href 整个URl字符串(在浏览器中就是完整的地址栏) 本例返回值: http://ifisker.com/b ...
- 如何使用网盘托管git项目
话说近年来git已经成为项目源代码管理的标准工具,有不少免费托管网站可供使用,详情参考这篇文章: http://www.cnblogs.com/zdz8207/archive/2012/05/20/2 ...
- ALTER 语句修改数据表
1.修改数据表名:alter table 表名 rename 新表名; 2.修改列名: alter table 表名 change 列名 新列名(可以与旧的一样) 类型 默认值; 3.修改类型: al ...
- 使用spring集成hibernate
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- JS 循环遍历JSON数据
JSON数据如:{"options":"[{/"text/":/"王家湾/",/"value/":/" ...
- 【VNC】Linux环境VNC服务安装、配置与使用
[VNC]Linux环境VNC服务安装.配置与使用 2009-06-25 15:55:31 分类: Linux 前言:作为一名DBA,在创建Oracle数据库的过程中一般要使用dbca和netc ...
- WSP (无线会话协议)
WSP (无线会话协议) WSP是在无线应用协议(WAP:Wireless Application Protocol )组中的协议,用两种服务提供无线应用环境一个稳定的接口. 中文名 WSP WAP ...
- CSS初学
CSS作用是美化html网页.内联:body 标签里.内嵌:head标签里,可以多次重复使用.p{}:引用时,class引用.#p{}:引用时,id引用超链接的stylea:link 超链接被点前的状 ...