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的更多相关文章

  1. leetcode@ [68] Text Justification (String Manipulation)

    https://leetcode.com/problems/text-justification/ Given an array of words and a length L, format the ...

  2. [LeetCode] 68. Text Justification 文本对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  3. LeetCode OJ——Text Justification

    http://oj.leetcode.com/problems/text-justification/ 编译代码要看warnings!它提供了可能出问题的情况,比如类型转换上unsigned int ...

  4. 【leetcode】Text Justification

    Text Justification Given an array of words and a length L, format the text such that each line has e ...

  5. 【leetcode】Text Justification(hard) ☆

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  6. [leetcode]68. Text Justification文字对齐

    Given an array of words and a width maxWidth, format the text such that each line has exactly maxWid ...

  7. Leetcode#68 Text Justification

    原题地址 没有复杂的算法,纯粹的模拟题 先试探,计算出一行能放几个单词 然后计算出单词之间有几个空格,注意,如果空格总长度无法整除空格数,前面的空格长度通通+1 最后放单词.放空格,组成一行,加入结果 ...

  8. Text Justification leetcode java

    题目: Given an array of words and a length L, format the text such that each line has exactly L charac ...

  9. [LeetCode] Text Justification 文本左右对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

随机推荐

  1. POJ-2777Count Color 线段树+位移

    这道题对于我这样的初学者还是有点难度的不过2遍A了还是很开心,下面说说想法-- Count Color Time Limit: 1000MS Memory Limit: 65536K Total Su ...

  2. codeforces 719B:Anatoly and Cockroaches

    Description Anatoly lives in the university dorm as many other students do. As you know, cockroaches ...

  3. spring全注解项目

    项目结构如下: spring配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns=&q ...

  4. android:sharedUserId 获取系统权限

    最近在做的项目,有好大一部分都用到这个权限,修改系统时间啊,调用隐藏方法啊,系统关机重启啊,静默安装升级卸载应用等等,刚开始的时候,直接添加权限,运行就报错,无论模拟器还是真机,在logcat中总会得 ...

  5. HD 1011 Starship Troopers(树上的背包)

    Starship Troopers Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  6. Why Deep Learning Works – Key Insights and Saddle Points

    Why Deep Learning Works – Key Insights and Saddle Points A quality discussion on the theoretical mot ...

  7. C语言产生随机数

    rand产生随机数 #include"stdio.h" #include"stdlib.h" void main() { int i; for(i=0;i< ...

  8. wes开发笔记

    html中的button和submit有什么不同? submit是提交表单用,而button是执行javascript用,两者各有用处. 用到自己写按钮的时候,都是用button,submit很少写 ...

  9. ubuntu查看版本命令

    有两种方法 1,cat /etc/issue 2,sudo lsb_release -a 这个查询出来的结果比上面的那个全一些.

  10. 微信新版支持读取iPhone M7/M8协处理器运动数据 与好友PK一下运动量吧

    iPhone的创新是有目共睹的,Healthkit的推出预示着苹果进军健康领域,iPhone M7/M8协处理器可以收集和分析用户的健康数据,那么好的硬件自然不会被势在打造完整生态圈的微信给错过,这不 ...