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;
    }
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章