zoukankan      html  css  js  c++  java
  • [LeetCode] 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 exactlyL 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.

    click to show corner cases.

    Corner Cases:
      • A line other than the last line might contain only one word. What should you do in this case?
        In this case, that line should be left-justified.
    Hide Tags
     String
     
    思路:在当前节点判断当前能否加入前面的line中,如果能加入(长度不超过L),就update len,如果不能,则计算当前start 到end 的字符串。处理字符串时注意两点,1. 当start==end时,2,当end==n-2,即当前line为最后一行时,另外注意一个技巧,因为我们在当前节点不能处理是,处理start到end,且end==i-1,所以我们要事先压入一个等于L长的字符串才能处理到原有的最后一个字符串。另外,处理完成后,要将i--,重新处理当前i,使其成为first。 整体上感觉不是很难,但要一次写对很难。
    class Solution {
        public:
            vector<string> fullJustify(vector<string> &words, int L)
            {
    
                words.push_back(string(L, 'a'));
                size_t n = words.size();
                int len = 0;
                bool isFirst = true;
                vector<string> res;
                int start = 0, end = 0;
                int remainder = 0;
                string str;
    
    
                for(int i = 0; i < n; i++)
                {
                    if(isFirst)
                    {
                        isFirst = false;
                        len = words[i].size();
                        start = i;
                        //cout << "start	" << start << endl;
                    }
                    else
                    {
                        if(words[i].size() + 1 + len <= L)
                            len += words[i].size() + 1;
                        else
                        {
                            end = i - 1;
                            remainder = L - len;
                            if(start == end) // only one world
                            {
                                str = words[start];
                                for(int j = 0; j < remainder; j++)
                                    str += ' ';
    
                                res.push_back(str);
                                isFirst = true;
                                str.clear();
                                len = 0;
    
                                i--; //handle from the start begin;
                                continue;
                            }
    
                            //cout << "start	" << start << endl;
                            //cout << "end	" << end << endl;
                            //cout << "n	" << n<< endl;
    
                            if(end == n-2) // the last line
                            {
                                for(int j = start; j <= end; j++)
                                {
                                    if(j == start)
                                        str += words[j];
                                    else
                                        str += ' ' + words[j];
                                }
                                for(int j = 0; j < remainder; j++)
                                    str += ' ';
    
                            }
                            else
                            {
                                int average = remainder/(end - start );
                                remainder = remainder%(end - start );
                                for(int j = start; j <= end; j++)
                                {
                                    if(j == start)
                                        str += words[j];
                                    else
                                    {
                                        int tmp = average;
                                        while(tmp > 0)
                                        {
                                            str += ' ';
                                            tmp--;
                                        }
                                        if(remainder > 0)
                                        {
                                            str += ' ';
                                            remainder --;
                                        }
                                        str += ' ';
                                        str += words[j];
                                    }
                                }
                            }
    
                            //cout << "str:" << str<< endl;
                            res.push_back(str);
                            isFirst = true;
                            str.clear();
                            len = 0;
    
                            i--; //handle from the start begin;
                        }
                    }
                    //cout << "len	" << len<< endl;
    
                }
    
                return res;
    
            }
    };
     
  • 相关阅读:
    程序员小抄大全
    赢在中国“80后30个忠告”
    Eclipse下python插件(pydev)的安装
    PDF加密文件的解密和打印
    中美欧联手打击僵尸网络 深化安全合作 狼人:
    入侵奥巴马帐号法国黑客自称是一个好黑客 狼人:
    Firefox 3.6.3版率先修复黑客大赛所曝漏洞 狼人:
    2009年全球安全SaaS市场收入比2008年增长70% 狼人:
    微软等厂商高管谈安全云面临的挑战 狼人:
    报告称Windows7不安全 管理员权限是罪魁祸首 狼人:
  • 原文地址:https://www.cnblogs.com/diegodu/p/4325269.html
Copyright © 2011-2022 走看看