zoukankan      html  css  js  c++  java
  • LeetCode OJ-- Text Justification

    https://oj.leetcode.com/problems/text-justification/

    细节题

    class Solution {
    public:
        vector<string> fullJustify(vector<string> &words, int L) {
            vector<string> ans;
            
            if(L == 0)
            {
                ans.push_back("");
                return ans;
            }
            
            int index = 0;
            int begin = 0;
            int tempLen = 0;
            int end = 0;
            int spaces = 0;
            int extraSpace = 0;
            while(index < words.size())
            {
                begin = index;
                tempLen = words[index].size();
                index++;
                while(index < words.size() && (tempLen + 1 + words[index].size()) <= L)
                {
                    tempLen = tempLen + 1 + words[index].size();
                    index++;
                }
                end = index - 1;
                string str;
                // not the last line
                if(end != words.size() - 1)
                {
                    // has more than one word
                    if(end != begin)
                    {
                        spaces = (L - tempLen) / (end - begin);
                        extraSpace = (L - tempLen) % (end - begin);
                        string strSpace;
                        for(int i = 0; i < spaces + 1; i++)
                        strSpace.append(" ");
                        for(int p = begin; p < end; p++)
                        {
                            str.append(words[p]);
                            str.append(strSpace);
                            if(p - begin < extraSpace)
                                str.append(" ");
                        }
                        str.append(words[end]);
                    }
                    else
                    {
                        str.append(words[begin]);
                        while(str.size() < L)
                            str.append(" ");
                    }
                }
                else
                {
                    for(int p = begin; p < end; p++)
                    {
                        str.append(words[p]);
                        str.append(" ");
                    }
                    str.append(words[end]);
                    while(str.size() < L)
                        str.append(" ");
                }
                ans.push_back(str);
            }
            return ans;
        }
    };
  • 相关阅读:
    npm常用命令
    关于事件委托和时间冒泡(以及apply和call的事项)
    js 杂记
    angular中关于ng-repeat的性能问题
    关于日期的一些东西
    杂记
    angular中关于自定义指令——repeat渲染完成后执行动作
    angular中事件戳转日期的格式
    ES6-promise
    angular中ng-class的一些用法
  • 原文地址:https://www.cnblogs.com/qingcheng/p/3913388.html
Copyright © 2011-2022 走看看