Text Justification,文本对齐

问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐。

words: ["This", "is", "an", "example", "of", "text", "justification."]
L: 16.

Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]
public class TextJustification {
	public ArrayList<String> fullJustify(String[] words, int L)
	{
	    ArrayList<String> res = new ArrayList<String>();
	    if(words==null || words.length==0)
	        return res;
	    int count = 0;//当前行字符串长度和
	    int last = 0;//每一行头元素下标
	    for(int i=0;i<words.length;i++)
	    {
	        //count是上一次计算的单词的长度,words[i].length()是当前尝试放的一个单词的长度,
	        //假设当前放上了这个单词,那么这一行单词跟单词间的间隔数就是i-last
	        //判断这些总的长度加起来是不是大于L(超行数了)
	        if(count + words[i].length() + (i-last) > L)
	        {
	            int spaceNum = 0;
	            int extraNum = 0;
	            //因为尝试的words[i]失败了,所以间隔数减1.此时判断剩余的间隔数是否大于0
	            if( i-last-1 >0)
	            {
	                //是间隔的倍数(为啥要减1,因为尝试当前words[i]后发现比L长了,
	                //所以当前这个单词不能算作这行,所以间隔就减少一个
	                spaceNum = (L-count)/(i-last-1);
	                extraNum = (L-count)%(i-last-1);//不是倍数的话还要计算
	            }
	            StringBuilder str = new StringBuilder();
	            for(int j=last;j<i;j++)
	            {
	                str.append(words[j]);
	                if(j<i-1)
	                {//words[i-1]的话后面就不用填空格了,所以这里j<i-1
	                    for(int k=0;k<spaceNum;k++)
	                        str.append(" ");

	                    if(extraNum>0)
	                    {
	                    	str.append(" ");
	                    	extraNum--;
	                    }
	                }
	            }

	            //下面这个for循环作用于一行只有一个单词还没填满一行的情况
	            for(int j=str.length();j<L;j++)
	                str.append(" ");

	            res.add(str.toString());
	            count=0;
	            last=i;//下一个开始的单词
	        }//if
	        count += words[i].length();
	    }//for

	    //处理最后一行
	    StringBuilder str = new StringBuilder();
	    for(int i=last;i<words.length;i++)
	    {
	        str.append(words[i]);
	        if(str.length()<L)
	            str.append(" ");
	    }
	    for(int i=str.length();i<L;i++)
	        str.append(" ");

	    res.add(str.toString());
	    return res;
	}
	public static void main(String[] args) {
		String[] words = {"This", "is", "an", "example", "of", "text", "justification."};
		int l = 16;
		TextJustification tj = new TextJustification();
		ArrayList<String> list = tj.fullJustify(words, l);
		for (String string : list) {
			System.out.println(string);
		}
	}
}
时间: 2024-10-10 06:49:48

Text Justification,文本对齐的相关文章

Leetcode 68. Text Justification 文本调整 解题报告

1 解题思想 这道题,其实我也想不通为什么要标记为Hard模式,题目的大意就是对一个字符串数组进行格式化调整,输出对应的句子. 要求有: 1.每一行的字符串长度不能超过一个固定长度maxWidth 2.每两个单词之间必须有一个空格,如果一行之间的单词之间空格不能细分,那么必须左边的空格多,右边的少.并且,空格多的地方只比右边少的多一个 3.最后一行不适用2的空格方式,正常的每个单词空一格就好,最后留白就好 提前贴个解释: * 这道题关键在于仔细的处理每一个步骤: * 1.每一行选择K的单词,K个

[leetcode]68. Text Justification文字对齐

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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

[Swift]LeetCode68. 文本左右对齐 | Text Justification

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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

LeetCode --- 68. Text Justification

题目链接: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

No.68 Text Justification

No.68 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

【一天一道LeetCode】#68. Text Justification

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

【leetcode】Text Justification

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 i

LeetCode: Text Justification [068]

[题目] 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. P

【LeetCode】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. P