`

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 Lcharacters.

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.

Corner Cases:
  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.
public List<String> fullJustify(String[] words, int L) {
    List<String> list = new ArrayList<>();
    int n = words.length;
    char[] spaces = new char[L];
    Arrays.fill(spaces, ' ');
    for(int i=0; i<n; i++) {
        int j = i; 
        int len = words[i].length();
        while(i<n-1 && len+1+words[i+1].length()<=L) {
            len += 1+words[++i].length();
        }
        //只有一个word或者是最后一行的时候,需要左对齐
        boolean left = (j==i || i==n-1);
        int avg = left ? 0 : (L-len) / (i-j); //平均空格个数-1
        int rem = left ? 0 : (L-len) % (i-j); //平均之后多出来的空格个数
        StringBuilder sb = new StringBuilder(words[j]);
        while(j < i) {
            sb.append(spaces, 0, avg+1);
            if(rem-- > 0) sb.append(' ');
            sb.append(words[++j]);
        }
        sb.append(spaces, 0, L-sb.length());
        list.add(sb.toString());
    }
    return list;
}

 

重构了一下Java代码:

public List<String> fullJustify(String[] words, int L) {
    List<String> res = new ArrayList<>();
    int n = words.length;
    char[] spaces = new char[L];
    Arrays.fill(spaces, ' ');
    for(int i=0; i<n; i++) {
        int len = words[i].length();
        int j = i;
        while(i<n-1 && len+1+words[i+1].length()<=L) {
            len += 1+words[++i].length();
        }
        StringBuilder sb = new StringBuilder(words[j]);
        if(j == i || i==n-1) {
            while(i==n-1 && j < i) {
                sb.append(" "+words[++j]);
            }
            sb.append(spaces, 0, L-sb.length());
        } else {
            int avg = (L-len)/(i-j);
            int rem = (L-len)%(i-j);
            while(j < i) {
                sb.append(spaces, 0, avg+1);
                if(rem-- > 0) sb.append(" ");
                sb.append(words[++j]);
            }
        }
        res.add(sb.toString());
    }
    return res;
}

 

C++的代码:

vector<string> fullJustify(vector<string>& words, int maxWidth) {
    vector<string> result;
    int i = 0, n = words.size();
    while(i < n) {
        int j(i), cnt(0), len(0);
        while(i<n && len+cnt+words[i].size()<=maxWidth) {
            len += words[i++].size();
            cnt++;
        }
        bool left = (cnt==1 || i==n); // should be left aligned or not
        int space = left ? 1 : (maxWidth-len) / (cnt-1);
        int rem   = left ? 0 : (maxWidth-len) % (cnt-1);
        string s;
        while(j < i) {
            s.append(words[j++]);
            if(j < i)
                s.append(space, ' ');
            if(rem > 0) {
                s.append(1, ' ');
                rem--;
            }
        }
        if(s.size() < maxWidth) {
            s.append(maxWidth-s.size(), ' ');
        }
        result.push_back(s);
    }
    return result;
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics