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;
        }
    };
    
  • 相关阅读:
    安装archlinux的另辟蹊径的命令及心得
    deepin15.11安装N卡驱动,实测!!!(可解决N卡电脑关机卡屏)
    js实现简单下载
    微信公众号的开发 该公众号提供的服务出现故障,请稍后再试
    线程池的创建
    多线程,生产者消费者模型(生产馒头,消费馒头)
    第1章 Java IO系统 下
    T01章[Java IO系统] 作业
    第1章 Java IO系统
    用集合实现一个控制台版的学生管理系统
  • 原文地址:https://www.cnblogs.com/aiterator/p/6698787.html
Copyright © 2011-2022 走看看