zoukankan      html  css  js  c++  java
  • 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;
        }
    };
    
  • 相关阅读:
    Python操作Excel
    unittest单元测试生成HTML测试报告
    pycharm安装 package报错:module 'pip' has no attribute 'main'
    Jenkins关闭、重启,Jenkins服务的启动、停止方法。
    selenium如何获取已定位元素的属性值?
    本周学习小结(23/03
    本周学习小结(16/03
    本周学习小结(09/03
    本周学习小结(02/03
    本周学习小结(24/02
  • 原文地址:https://www.cnblogs.com/aiterator/p/6698787.html
Copyright © 2011-2022 走看看