• 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."]

    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.

    就是一直读取就好了,直到如果读取下一个会大于maxWidth时停止读取。

    需要注意的是每行一个和最后一行的排列。

    class Solution {
    public:
        vector<string> fullJustify(vector<string>& words, int maxWidth) {
        	vector<string> ans;
            if(words.size() == 0)
            	return ans;
    
            for(int i=0; i < words.size();)
            {
            	int res = 0, s = 0;
            	while(i < words.size() && res + words[i].size() + 1 <= maxWidth)
            		res += words[i ++].size() + 1, s ++;
            	if(i < words.size() && res + words[i].size() == maxWidth)
            	{
            		res += words[i ++].size(), s ++;
            		string str;
            		for(int j=0; j<s-1; ++ j)
            			str += words[i-s+j] + ' ';
            		str += words[i-1];
            		ans.push_back(str);
            		continue;
            	}
    
            	if(s == 1)
            	{
            		string str(words[i-1]);
            		str += string(maxWidth-words[i-1].size(), ' ');
            		ans.push_back(str);
            		continue;
            	}
    
            	if(i < words.size())
            	{
            		int f = maxWidth - res;
    	        	int x = (f + s) / (s-1), y = (f + s) % (s-1);
    	        	string str;
    	        	for(int j=0; j<y; ++ j)
    	        		str += words[i-s+j] + string(x+1, ' ');
    	        	for(int j=y; j<s-1; ++ j)
    	        		str += words[i-s+j] + string(x, ' ');
    	        	str += words[i-1];
    	        	ans.push_back(str);
            	}
            	else
            	{
            		int x = 0;
            		string str;
            		for(int j=0; j<s-1; ++ j)
            			str += words[i-s+j] + ' ', x += words[i-s+j].size() + 1;
            		str += words[i-1], x += words[i-1].size();
            		str += string(maxWidth-x, ' ');
            		ans.push_back(str);
            	}
            }
            return ans;
        }
    };
    
  • 相关阅读:
    hdu 2444 交叉染色判断二分图+二分最大匹配
    uva 交叉染色法10004
    poj 3177&&3352 求边双联通分量,先求桥,然后求分量( 临界表代码)
    poj 3177&&poj 3352加边构双联通(有重边)用tarjan 模板求的
    poj 3006水题打素数表
    POJ 3352 无向图边双连通分量,缩点,无重边
    hdu 1430 魔板 康托展开 + 很好的映射
    D. Artsem and Saunders 数学题
    vijos P1412多人背包 DP的前k优解
    1475 建设国家 DP
  • 原文地址:https://www.cnblogs.com/aiterator/p/6698787.html
Copyright © 2020-2023  润新知