LeetCode 题解(103): Text Justification

来源:互联网 发布:实战nginx 取代 编辑:程序博客网 时间:2024/06/04 17:49

题目:

Given an array of words and a length L, format the text such that each line has exactlyL 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.

题目:

没有算法,暴力求解。

c++版:

class Solution {public:vector<string> fullJustify(vector<string>& words, int maxWidth) {vector<string> result;if (words.size() == 0 || maxWidth == 0) {result.push_back("");return result;}int i = 0;while (i < words.size()) {int end = i;int remain = maxWidth;string s = "";while (remain > 0 && remain - (int)words[end].length() >= 0) {//s += words[end];//s += " ";if (end != i) {    if (remain - (int)words[end].length() == 0 && end + 1 != words.size())    break;remain -= 1;}remain -= words[end++].length();if (end >= words.size()) break;}if (remain < 0) {remain += (words[--end].length() + 1);//s = s.substr(0, s.find(words[end])-1);}if (end < words.size()) {if (end - i - 1 != 0) {int each = remain / (end - i - 1) + 1;int r = remain % (end - i - 1);for (int j = i; j < end; j++) {s.append(words[j]);if (j != end - 1) {for (int k = 0; k < each; k++)s.append(" ");if (j - i + 1 <= r)s.append(" ");}}}else {s.append(words[i]);for (int k = 0; k < remain; k++)s.append(" ");}}else {for (int j = i; j < end; j++) {s.append(words[j] + " ");}s = s.substr(0, s.length() - 1);for (int k = 0; k < remain; k++)s.append(" ");}result.push_back(s);i = end;}return result;}};

Java版;

public class Solution {    public List<String> fullJustify(String[] words, int maxWidth) {        List<String> result = new ArrayList<String>();        if(words.length == 0 || maxWidth == 0) {            result.add("");            return result;        }        int i = 0;        while(i < words.length) {            int remain = maxWidth;            int end = i;            while(remain > 0 && remain - words[end].length() >= 0) {                if(end != i) {                    if(remain - words[end].length() == 0 && end + 1 != words.length)                        break;                    remain--;                }                remain -= words[end++].length();                if(end == words.length)                    break;            }            if(remain < 0) {                remain += (words[--end].length() + 1);            }            if(end < words.length) {                if(end - i - 1 != 0) {                    int gap = remain / (end - i - 1) + 1;                    int r = remain % (end - i - 1);                    StringBuilder s = new StringBuilder();                    for(int j = i; j < end; j++) {                        s.append(words[j]);                        if(j != end - 1) {                            for(int k = 0; k < gap; k++)                                s.append(" ");                            if(j - i + 1 <= r)                                s.append(" ");                        }                    }                    result.add(s.toString());                } else {                    StringBuilder s = new StringBuilder();                    s.append(words[i]);                    for(int j = 0; j < remain; j++)                        s.append(" ");                    result.add(s.toString());                }            } else {                StringBuilder s = new StringBuilder();                for(int j = i; j < end; j++) {                    s.append(words[j]);                    s.append(" ");                }                s.delete(s.length()-1, s.length());                for(int k = 0; k < remain; k++)                     s.append(" ");                result.add(s.toString());            }            i = end;        }        return result;    }}

Python版:

class Solution:    # @param {string[]} words    # @param {integer} maxWidth    # @return {string[]}    def fullJustify(self, words, maxWidth):        result = []        if len(words) == 0 or maxWidth == 0:            result.append("")            return result        i = 0        while i < len(words):            end = i            remain = maxWidth            while remain > 0 and remain - len(words[end]) >= 0:                if end != i:                    if remain - len(words[end]) == 0 and end + 1 == len(words):                        break;                    remain -= 1                remain -= len(words[end])                end += 1                if end == len(words):                    break            if remain < 0:                end -= 1                remain += (len(words[end]) + 1)            if end != len(words):                if end - i - 1 != 0:                    each = remain / (end - i - 1) + 1                    r = remain % (end - i - 1)                    s = ""                    for j in range(i, end):                        s += words[j]                        if j != end - 1:                            for k in range(0, each):                                s += " "                            if j - i + 1 <= r:                                s += " "                    result.append(s)                else:                    s = words[i]                    for j in range(0, remain):                        s += " "                    result.append(s)            else:                s = ""                for j in range(i, end):                    s += words[j]                    s += " "                s = s[:-1]                for k in range(0, remain):                    s += " "                result.append(s)            i = end        return result


0 0