Text Justification

本文介绍了一种方法,通过贪婪算法实现文本在给定长度下的多行排列与格式化,确保每行字符均匀分布且符合左右对齐规则。以示例展示如何将单词序列调整为指定宽度的完美布局。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 Solution {
    public ArrayList<String> fullJustify(String[] words, int L) {
		// Start typing your Java solution below
		// DO NOT write main() function
		ArrayList<String> res = new ArrayList<String>();
		if (words.length == 0)
			return res;
		int index = 0;
		StringBuilder sb = new StringBuilder();
		while (index < words.length) {
			sb = new StringBuilder();
			int volume = 0; // number of chars this line
			int cur_len = 0;// number of chars plus one space for each word
			int counter = 0;
			int initial = index;
			while (index < words.length && words[index].length() + cur_len <= L) {
				cur_len += (words[index].length() + 1);// at least one space
				volume += words[index].length();
				counter++;
				index++;
			}
			if(index == words.length){// if last line, all words go left
				while(initial < index){
					sb.append(words[initial++]);
					if(sb.length() < L)
						sb.append(" ");// one space per word, if length permits for the last word
				}
				while(sb.length() < L)
					sb.append(" ");// add spaces till end of last line
				res.add(sb.toString());
				break;
			}
			int spaces = L - volume;
			if (counter != 1) {
				int spaces_per_word = spaces / (counter - 1);
				int extra_spaces = spaces % (counter - 1);
				while(initial < index){// no "=", since index points to the next word now
					sb.append(words[initial++]);
					if(--counter == 0)//the last word does not need space
						break;
					if(extra_spaces > 0){
						for(int i = 0; i < spaces_per_word; i++)
							sb.append(" ");
						sb.append(" ");// extra one apace for each word
						extra_spaces--;
					}
					else{
						for(int i = 0; i < spaces_per_word; i++)
							sb.append(" ");
					}
				}
			}
			else{// if only one word this line, all spaces go right
				sb.append(words[initial]);
				for(int i = 0; i < spaces; i++)
					sb.append(" ");
			}
			res.add(sb.toString());
		}
		return res;
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值