• Leetcode:68. Text Justification


    Description

    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.

    Example

    For example,
    words: ["This", "is", "an", "example", "of", "text", "justification."]
    L: 16.

    Return the formatted lines as:

    [
        "This    is    an",
        "example  of text",
        "justification.  "
    ]
    

    思路

    • 首先,根据贪心策略,找到此次最多能包含的单词,注意,单词直接要加一个空格
    • 找出之后,根据长度的差值计算应该添加的空格的数目

    代码

    class Solution {
    public:
      vector<string> fullJustify(vector<string>& words, int maxWidth) {
    		int len = words.size();
    		vector<string> res;
    		if (len == 0) return res;
    
    		vector<int> nums(len, 0);
    		int i = 0;
    		for (i = 0; i < len; ++i)
    			nums[i] = words[i].size();
    
    		i = 0;
    		int count = 0;
    		while (i < len){
    			string tmp;
    
    			int start = i;
    			getPadSpaceNum(nums, i, len, maxWidth);
    			for (int j = start; j <= i; ++j){
    				tmp += words[j];
    				if (j < i )
    					tmp += string(nums[j], ' ');
    			}
                if(tmp.size() < maxWidth)
                    tmp += string(maxWidth - tmp.size(), ' ');
                    
    			res.push_back(tmp);
    			i++;
    		}
    
    		return res;
    	}
    
    private:
        //vector<int> nums, 传入时保存的是对应单词长度,
        //函数返回后保存的是对应单词其后加入的空格个数
    	void getPadSpaceNum(vector<int>& nums, int &i, int len, int maxWidth){
    		int s = i;
    		int sum = nums[i];
    		int count = 0;
    		while (i + 1 < len && sum + 1 + nums[i + 1] <= maxWidth){
    			sum += 1 + nums[i + 1];
    			i++;
    			count++;   //count记录需要加入空格的位置的个数
    		}
    
    		int sub = maxWidth - sum;
    		if (sub == 0 || i == len - 1){
    			for (int j = s; j <= i; ++j)
    				nums[j] = 1;
    		}
    		else{
    			int tmp = 0;
    			if (count){
    				tmp = sub / count + 1;
    				count = sub % count;
    			}
    				
    			if (tmp == 0) tmp = 1;
    
    			
    			for (int j = s; j <= i; ++j){
    				if (count > 0){
    					nums[j] = tmp + 1;
    					count--;
    				}
    				else nums[j] = tmp;
    			}
    		}
    	}
    };
    
  • 相关阅读:
    基于lua语言实现面向对象编程
    一.Linux常用命令
    获取线程名称、设置线程名称、获取当前所有线程
    关系型数据库和非关系数据库区别
    Java基础类型之间的转换
    初始化 List 的几种方法
    谷歌浏览器打不开网页,但Opera可以打开网页
    遍历List和Map的几种方法
    java对数组进行排序
    MySQL实现事务隔离的原理:MVCC
  • 原文地址:https://www.cnblogs.com/lengender-12/p/6920497.html
Copyright © 2020-2023  润新知