leetCode练习(68)
来源:互联网 发布:电吉他入门 知乎 编辑:程序博客网 时间:2024/06/10 06:36
题目:Text Justification
难度:hard
问题描述:
Given an array of words and a length L, format the text such that each line has exactlyL 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
.
Return the formatted lines as:
[ "This is an", "example of text", "justification. "]
Note: Each word is guaranteed not to exceed L in length.
解题思路:按照脑子里想的步骤做就行,但是很多细节和边界要注意,没有找到啥捷径嘿嘿~
主要就是一行一行填进去。
具体代码如下:
public class h_68_TextJustification {public static List<String> fullJustify(String[] words, int maxWidth) {List<String> list=new ArrayList<>();if(maxWidth==0){list.add("");return list;}while(words!=null){words=putline(words,maxWidth,list);if(words!=null){//show(words);}}return list; }public static void show(String[]words){for(int i=0;i<words.length;i++){System.out.print(words[i]+":");}System.out.println();}public static String[] putline(String[]words,int max,List<String> list){if(words.length==0)return null;int templen=-1;int i;int num=-1;String[] res;String str=words[0]; for(i=0;i<words.length;i++){ templen+=words[i].length()+1; if(templen>max){ num=i; templen-=words[i].length(); break; } } if(num==-1){//最后一行 num=words.length; for(i=1;i<num;i++){ str+='.'; str+=words[i]; } for(i=str.length();i<max;i++){ str+='.'; } System.out.println(str); list.add(str); return null; } //有num-1个间隔 space个空格 int jiange=num-1; if(num==1){ for(i=words[0].length();i<max;i++){ str+='.'; } System.out.println(str); list.add(str); if(words.length==1){ return null; }else{ res=new String[words.length-num]; System.arraycopy(words, num, res, 0, res.length); return res; } } int space=max-templen+i; int leftnum=space-space/jiange*jiange; int minspace=space/jiange; //先是leftnum个(minspace+1)长的空格 再来(jiange-leftnum)个(minspace)长的空格 String ko=""; for(i=0;i<minspace;i++){ ko+='.'; } for(i=0;i<leftnum;i++){ str+=(ko+'.'); str+=words[i+1]; } for(i=0;i<(jiange-leftnum);i++){ str+=ko; str+=words[i+leftnum+1]; } System.out.println(str); list.add(str); res=new String[words.length-num]; System.arraycopy(words, num, res, 0, res.length); return res;}public static void main(String[]args){ArrayList<String> list=new ArrayList<>();String[] nums=new String[7];nums[0]="a";nums[1]="b";nums[2]="c";nums[3]="d";nums[4]="e";nums[5]="f";nums[6]="g";String[] nums2=new String[1];nums2[0]="a";//nums2[1]="b";fullJustify(nums,1);//putline(nums2,3,list);}}
- leetCode练习(68)
- leetCode练习(7)
- leetCode练习(9)
- leetCode练习(2)
- leetCode练习(14)
- leetCode练习(3)
- leetCode练习(4)
- leetCode练习(19)
- leetCode练习(18)
- leetCode练习(5)
- leetCode练习(15)
- leetCode练习(20)
- leetCode练习(21)
- leetCode练习(16)
- leetCode练习(17)
- leetCode练习(11)
- leetCode练习(24)
- leetCode练习(22)
- Java的SPI机制
- JPA和hibernate关系
- 深入解析GDAL库的RasterIO()函数
- ffmepg源码中的 -re
- HTML5学习_day12(2)--流式布局
- leetCode练习(68)
- Mac终端命令
- 第四周项目4——建设双链表算法库
- New IO JAVA1.4特性
- Leetcode 113 Path Sum II
- php图片压缩
- js正则表达式语法
- 使用FormData对象提交表单
- 10-11