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 ...
随机推荐
- 加载数据库驱动程序的方法和JDBC的流程
加载驱动方法 1.Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); 2. DriverManager.r ...
- Day1 三级目录
d_city = { "河南" : {"郑州" : ["二七区","中原区","回族管城区",&qu ...
- BZOJ-2257 瓶子和燃料 分解因数+数论方面乱搞(裴蜀定理)
一开始真没想出解法...后来发现那么水.... 2257: [Jsoi2009]瓶子和燃料 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 970 So ...
- BZOJ-1927 星际竞速 最小费用最大流+拆点+不坑建图
1927: [Sdoi2010]星际竞速 Time Limit: 20 Sec Memory Limit: 259 MB Submit: 1593 Solved: 967 [Submit][Statu ...
- NOIP2013 货车运输 (最大生成树+树上倍增LCA)
死磕一道题,中间发现倍增还是掌握的不熟 ,而且深刻理解:SB错误毁一生,憋了近2个小时才调对,不过还好一遍AC省了更多的事,不然我一定会疯掉的... 3287 货车运输 2013年NOIP全国联赛提高 ...
- junit加载
Run as junit 不会出现,把junit 包倒入lib文件夹中,在类的后面加上extends TestCase,此时上方会导入一个包:import junit.framework.TestCa ...
- 排序算法二(时间复杂度为O(N*logN))
快速排序: 1 package test; public class QuickSort { // 快速排序 public void quickSort(int s[], int l, int r) ...
- 基础总结篇之三:Activity的task相关
http://blog.csdn.net/liuhe688/article/details/6761337 古人學問無遺力,少壯工夫老始成.紙上得來終覺淺,絕知此事要躬行.南宋.陸遊<冬夜讀書示 ...
- emmet使用 及 notepadd++ emmet的安装
emmet的使用的参考文章:http://www.cnblogs.com/sussski/p/3544744.html html:4s.html:4t.html:5或! +.>.^:层次 *.@ ...
- Ten Tips for Writing CS Papers, Part 1
Ten Tips for Writing CS Papers, Part 1 As a non-native English speaker I can relate to the challenge ...