leetCode练习(68)

题目:Text Justification

难度:hard

问题描述:

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.

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);
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值