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

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

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.

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.

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.

这道题正确地处理各种计数,再分情况处理就可以了。

主要分三种情况:

1. 只有一个单词。左对齐,右补空白;

2. 最后一行,单词之间只有一个空白;

3. 其他行,多个单词,那么就要平均空白;如果不能平均,那么就取余数,前几个就要多一个空白,尽量平均;(Line 31-35)

在计算是否能够容纳单词的时候,要算上空白。所以用了一个length计算每个单词之间至少一个空白的总长度,再用sum计算只有单词的总长度,方便计算空格数。(Line 17-18)

1 class Solution { 2 public: 3     vector
fullJustify(vector
&words, int L) { 4 vector
ret; 5 int n = words.size(); 6 if (n == 0) return ret; 7 if (L <= 0) { 8 ret.push_back(""); 9 return ret;10 }11 12 int s = 0, e = s, count, pad, sum, length, left;13 while (true) {14 if (s >= n) break;15 sum = length = words[s].length();16 for (e = s + 1; e < n && (length + words[e].length() + 1) <= L; ++e) {17 sum += words[e].length();18 length += 1 + words[e].length();19 }20 count = e - s;21 string tmp(words[s]);22 if (count == 1) {23 tmp.append(L - sum, ' ');24 } else if (e == n) { 25 for (int i = s + 1; i < e; ++i) {26 tmp += " " + words[i];27 }28 tmp.append(L - sum - count + 1, ' ');29 } else {30 pad = L - sum;31 left = pad % (count - 1);32 pad = pad / (count - 1);33 for (int i = s + 1; i < e; ++i) {34 tmp.append(pad, ' ');35 if (left-- > 0) tmp.append(1, ' ');36 tmp += words[i];37 }38 }39 ret.push_back(tmp);40 s = e;41 }42 43 return ret;44 }45 };

 第三次写,一开始就把只含一个数的情况单独考虑,然后再把中间行和最后一行分开考虑,这样写清晰许多。

1 class Solution { 2 public: 3     vector
fullJustify(vector
&words, int L) { 4 vector
ans; 5 int n = words.size(); 6 for (int start = 0; start < n; ) { 7 if (words[start].length() == L || start == n - 1 8 || words[start].length() + words[start + 1].length() + 1 > L) { // only one element 9 if (words[start].length() < L) words[start].append(L - words[start].length(), ' ');10 ans.push_back(words[start++]);11 } else { // more than one element12 int end = start, len = -1;13 for (; end < n && len + words[end].length() + 1 <= L; len += words[end].length() + 1, ++end);14 stringstream ss;15 if (end == n) { // last line16 for (int i = start; i < end - 1; ++i) {17 ss << words[i] << " ";18 }19 if (L - len > 0) words[end - 1].append(L - len, ' ');20 ss << words[end - 1];21 } else { // middle lines22 int space = (L - len) / (end - start - 1), extra = (L - len) % (end - start - 1);23 for (int i = start; i < end - 1; ++i) {24 string spaces = "";25 spaces.append(space + 1, ' ');26 if (extra-- > 0) spaces.append(1, ' ');27 ss << words[i] << spaces;28 }29 ss << words[end - 1];30 }31 ans.push_back(ss.str());32 start = end;33 }34 }35 36 return ans;37 }38 };

 

转载于:https://www.cnblogs.com/linyx/p/3721291.html

你可能感兴趣的文章
MyBatis-3.4.2-源码分析11:XML解析之environmentsElement+Druid的解析准备工作:整合Druid...
查看>>
word 里输入法不见了怎么办
查看>>
CentOS 7.4 安装mysql5.6(yum)
查看>>
linux下网络流量监控工具
查看>>
6步在CentOS安装和配置MariaDB MySQL
查看>>
TP_框架下的GD图片处理类(含基本php图片处理思路)
查看>>
在浏览器中使用NPM模块,使用Browserify、Webpack导出
查看>>
压力测试 apache ab工具使用说明
查看>>
CentOS6.3源码安装mysql5.5.28
查看>>
intel笔记本cpu型号后缀详解(M,U,QM,MQ,HQ,XM)
查看>>
Iperf使用方法与参数说明
查看>>
【技术教程】MySQL to SequoiaDB数据迁移
查看>>
Citrix XenMobile学习笔记之六:XenMoble业务访问数据流程
查看>>
访问cacti的首页面为空白
查看>>
ssh不能登陆了,openssl库冲突
查看>>
Jenkins + maven + git 多环境自动化部署
查看>>
YII2 日志模块 之 使用数据库记录错误信息
查看>>
程序员的四个境界
查看>>
条款16:成对使用new和delete时要采取相同的形式
查看>>
LRU(Least Recent Used) java 实现为这么采用HashMap+双向链表
查看>>