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;
        }
    };
    
  • 相关阅读:
    JS 按钮下一步(onclick点击事件)
    socketserver模块
    进程
    僵尸进程和孤儿进程
    守护进程
    互斥锁
    进程间通信=>IPC机制
    生产者消费者模型
    线程
    守护线程
  • 原文地址:https://www.cnblogs.com/aiterator/p/6698787.html
Copyright © 2011-2022 走看看