LeetCode 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 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."], L16.
    Return the formatted lines as:

[
   "This    is    an",
   "example  of text",
   "justification.  "
]

    Note: Each word is guaranteed not to exceed L in length.

分析:
    题意:给定一个字符串数组words,一个限定长度L,我们按照以下规则对字符串数组进行格式调整:①在限定长度范围内L,用至少一个、尽量均匀的空格连接尽多的单词;②某两个单词长度和超过L,将前一个单词单独处理,后面用空格补齐至L
    思路:这是一道模拟题,需要按照题目要求去进行字符串连接。假设字符串数组大小为n,新的字符串连接开始下标start = 0,新的字符串累加长度len = 0,步骤如下:① 先获得所有字符串单词的最长长度maxLength,如果maxLength > L,那么无法进行连接,直接返回原数组;② 对于i∈0→n - 1,找到第一个i,使得字符串连接累加长度len + (i - start) > L(i - start的含义为,start→i共(i - start + 1)个字符串之间至少需要(i-start)个空格连接),那么start→i - 1之间,需要填充delta = L- len个空格,Ⅰ. 如果 i - 1 - start = 0,说明只有一个字符串需要处理,直接在右边加上delta个空格即可;Ⅱ.如果i - 1 - start > 0, 则q = delta / (i - 1 - start),r = delta % (i - 1 - start),其中q个空格均匀分配,r个空格从左往右逐一分配、分完为止;处理完当前连接字符串,start = i,len = 0,继续进入循环,处理后续字符串。
    处理完成后,返回结果。
    时间复杂度为O(n)。

代码:

#include <bits/stdc++.h>

using namespace std;

class Solution {
public:
    vector<string> fullJustify(vector<string>& words, int maxWidth) {
        vector<string> ans;
		int n = words.size();
		// Exceptional Case: 
		if(n == 0){
			return ans;
		}
		// get max word length
		int maxLength = 0;
		for(string str: words){
			int str_len = str.length();
			maxLength = max(maxLength, str_len);
		}
		if(maxLength > maxWidth){
			return ans;
		}
		int len = 0, start = 0;
		int delta, q, r;
		string res;
		for(int i = 0; i <= n - 1; i++){
			len += words[i].length();
			if(len + i - start > maxWidth){
				res = "";
				len -= words[i].length();
				delta = maxWidth - len;
				if(i - 1 - start == 0){
					res += (words[i - 1] + string(delta, ' '));
				}
				else{
					q = delta / (i - 1 - start);
					r = delta % (i - 1 - start);
					for(int j = start; j <= i - 2; j++){
						res += words[j];
						if(r > 0){
							res += string(q + 1, ' ');
							r--;
						}
						else{
							res += string(q, ' ');
						}
					}
					res += words[i - 1];					
				}
				ans.push_back(res);
				len = 0;
				start = i--;
			}
		}
		res = "";
		for(int i = start; i <= n - 2; i++){
			res += (words[i] + " ");
		}
		res += (words[n - 1] + string(maxWidth - len - (n - 1 - start), ' '));
		ans.push_back(res);
		return ans;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值