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;
        }
    };
    
  • 相关阅读:
    mouseover、mouseout,mouseenter、mouseleave区别
    第一篇博客,就真的是随笔,写写最近的状况。
    MySQL日期时间函数大全 转
    解决Eclipse中SVN版本信息不显示的问题
    android 环境变量配置,以及sdcard配置
    服务器Out of Memory
    Android SDK Manager 更新时的“https://dl-ssl.google.com refused”错误
    不可变的原始值和可变的对象引用
    null和undefined
    HTML 表单
  • 原文地址:https://www.cnblogs.com/aiterator/p/6698787.html
Copyright © 2011-2022 走看看