博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode:Text Justification
阅读量:5245 次
发布时间:2019-06-14

本文共 2899 字,大约阅读时间需要 9 分钟。

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 Lcharacters.

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.

 

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.

分析:这一题需要注意两个点,a、当该行只放一个单词时,空格全部在右边 b、最后一行中单词间只有一个空格,其余空格全部在右边。然后只要贪心选择,在一行中尽量放多的单词。                                                                                            

class Solution {public:    vector
fullJustify(vector
&words, int L) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. vector
res; int len = words.size(), i = 0; while(i < len) { //if(words[i].size() == 0){i++; continue;} int rowlen = 0, j = i; while(j < len && rowlen + words[j].size() <= L) rowlen += (words[j++].size() + 1); //j-i是该行放入单词的数目 if(j - i == 1) {//处理放入一个单词的特殊情况 res.push_back(words[i]); addSpace(res.back(), L - words[i].size()); i = j; continue; } //charaLen是当前行字母总长度 int charaLen = rowlen - (j - i); //平均每个单词后的空格,j < len 表示不是最后一行 int meanSpace = j < len ? (L - charaLen) / (j - i - 1) : 1; //多余的空格 int leftSpace = j < len ? (L - charaLen) % (j - i - 1) : L - charaLen - (j - i -1); string tmp; for(int k = i; k < j - 1; k++) { tmp += words[k]; addSpace(tmp, meanSpace); if(j < len && leftSpace > 0) { tmp.push_back(' '); leftSpace--; } } tmp += words[j - 1];//放入最后一个单词 if(leftSpace > 0)addSpace(tmp, leftSpace); //对最后一行 res.push_back(tmp); i = j; } return res; } void addSpace(string &s, int count) { for(int i = 1; i <= count; i++) s.push_back(' '); }};

 

【版权声明】转载请注明出处

转载于:https://www.cnblogs.com/TenosDoIt/p/3475275.html

你可能感兴趣的文章
PS 滤镜— — sparkle 效果
查看>>
snmpwalk命令常用方法总结
查看>>
网站产品设计
查看>>
代理ARP
查看>>
go 学习笔记(4) ---项目结构
查看>>
java中静态代码块的用法 static用法详解
查看>>
Java线程面试题
查看>>
Paper Reading: Relation Networks for Object Detection
查看>>
day22 01 初识面向对象----简单的人狗大战小游戏
查看>>
mybatis源代码分析:深入了解mybatis延迟加载机制
查看>>
Flask三剑客
查看>>
Hibernate-缓存
查看>>
【BZOJ4516】生成魔咒(后缀自动机)
查看>>
提高PHP性能的10条建议
查看>>
svn“Previous operation has not finished; run 'cleanup' if it was interrupted“报错的解决方法...
查看>>
熟用TableView
查看>>
Java大数——a^b + b^a
查看>>
poj 3164 最小树形图(朱刘算法)
查看>>
服务器内存泄露 , 重启后恢复问题解决方案
查看>>
android一些细节问题
查看>>