LeetCode 68. Text Justification

文档适配?就类似word文档左右顶格。哇,自己做出一道hard还是有点开心的。


Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth 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.(单词之间至少要有一个空格,空格不够均匀分配时,左边空多一点,也就是14个空格5个word的话,会依次空4,4,4,3格)

For the last line of text, it should be left justified and no extra space is inserted between words. (最后一行text向左对齐)

 

Note:

  • A word is defined as a character sequence consisting of non-space characters only. (一个word中间不会有空格)
  • Each word's length is guaranteed to be greater than 0 and not exceed maxWidth. (0<单词长度<maxWidth)
  • The input array words contains at least one word. (输入至少有一个单词)

Example 1:

Input:
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
Output:
[
   "This    is    an",
   "example  of text",
   "justification.  "
]

Example 2:

Input:
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
Output:
[
  "What   must   be",
  "acknowledgment  ",
  "shall be        "
]
Explanation: Note that the last line is "shall be    " instead of "shall     be",
             because the last line must be left-justified instead of fully-justified.
             Note that the second line is also left-justified becase it contains only one word.

Example 3:

Input:
words = ["Science","is","what","we","understand","well","enough","to","explain",
         "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
Output:
[
  "Science  is  what we",
  "understand      well",
  "enough to explain to",
  "a  computer.  Art is",
  "everything  else  we",
  "do                  "
]

C++版本。

#include<iostream>
#include<vector>
using namespace std;

string blanks(int n){//输出空格 
	string s="";
	for(int i=0;i<n;i++){
		s+=" ";
	}	
	return s;
}
//处理生成string 
string dealString(vector<string> words, int maxWidth){
	string result;
	int word_num = words.size();
	string s="";
	int blank_num = word_num - 1;
	//如果只有一个字符串 ,只需要在后面加空格 
	if(blank_num == 0){
		s += words[0]+blanks(maxWidth-words[0].length());
		return s;
	}
	//如果多个字符串 ,计算空格的数量和长度 
	int cnt = 0;
	for(int i=0;i<word_num;i++){
		cnt += words[i].length();
	}
	int blank_sum = maxWidth - cnt;
	int blank_len = blank_sum/blank_num;//15/4=3
	int blank_end = blank_sum%blank_num;//15%4=3
	
	string blank=blanks(blank_len);		//3个空格 
	for(int i=0;i<words.size();i++){
		s += words[i]; 
		if(i==words.size()-1 && words.size()>1) break;//结尾不需要空格 
		s += blank;
		if(blank_end>0) {
			s += " ";					//4个空 
			blank_end--;
		}
	}

	return s;
}
vector<string> fullJustify(vector<string>& words, int maxWidth) {
    int len = words.size();
    vector<string> result;
    vector<string> stmp;
    int count=0;
    //处理每一行(除了最后一行)放进一个vector中调用上面函数处理 
    for(int i=0;i<len;i++){
    	string tmp = words[i];
    	int slen = tmp.length();
    	if(count+slen>maxWidth){		//这一行满了 
    		string str = dealString(stmp,maxWidth);
    		stmp.clear();
    		count = 0;
    		result.push_back(str);
    	}
    	if(count+slen==maxWidth) count+=slen;
		else count += slen+1;
    	stmp.push_back(tmp);
    }
    //处理最后一行string 
    int count_str=0;
    string strtmp="";
    for(int i=0;i<stmp.size();i++){
    	count_str+=stmp[i].length();
    	strtmp += stmp[i];
    	if(i<stmp.size()-1){
    		strtmp += " ";
    		count_str++;
    	}else{
    		strtmp += blanks(maxWidth-count_str);
    	}
    }
    result.push_back(strtmp);
    return result;
}

int main(){
	vector<string> words;
	words.push_back("This");
	words.push_back("is");
	words.push_back("an");
	words.push_back("example");
	words.push_back("of");
	words.push_back("text");
	words.push_back("justification.");

	int maxWidth = 17;
	vector<string> d = fullJustify(words,maxWidth);
	for(int i=0;i<d.size();i++){
		cout<<d[i]<<endl;
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值