68. 文本左右对齐

给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ’ ’ 填充,使得每行恰好有 maxWidth 个字符。

要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

文本的最后一行应为左对齐,且单词之间不插入额外的空格。

说明:

单词是指由非空格字符组成的字符序列。

每个单词的长度大于 0,小于等于 maxWidth。

输入单词数组 words 至少包含一个单词。

示例:

输入:

words = [“This”, “is”, “an”, “example”, “of”, “text”, “justification.”]

maxWidth = 16

输出:

[

“This is an”,

“example of text”,

"justification. "

]

示例 2:

输入:

words = [“What”,“must”,“be”,“acknowledgment”,“shall”,“be”]

maxWidth = 16

输出:

[

“What must be”,

"acknowledgment ",

"shall be "

]

解释: 注意最后一行的格式应为 "shall be " 而不是 “shall be”,

因为最后一行应为左对齐,而不是左右两端对齐。

第二行同样为左对齐,这是因为这行只包含一个单词。

示例 3:

输入:

words = [“Science”,“is”,“what”,“we”,“understand”,“well”,“enough”,“to”,“explain”,

“to”,“a”,“computer.”,“Art”,“is”,“everything”,“else”,“we”,“do”]

maxWidth = 20

输出:

[

“Science is what we”,

“understand well”,

“enough to explain to”,

“a computer. Art is”,

“everything else we”,

"do "

]

class Solution {

    public List<String> fullJustify(String[] words, int maxWidth) {
List<String> ret = new ArrayList<>(); int index = 0;
while(index < words.length){
int cur = index, len = 0;
// len + words[cur].length() + cur - index 为单词之间取 一个空格的长度
while(cur < words.length && len + words[cur].length() + cur - index <= maxWidth){
// 计算纯单词长度
len = len + words[cur++].length();
}
cur--;
// System.out.println(cur + " " + len);
StringBuilder sb = new StringBuilder();
// 区分最后一行
if(cur == words.length - 1){
for(int i = index; i <= cur; i++){
sb.append(words[i]);
if(i < cur){
sb.append(' ');
}
}
}else{
int base = cur > index ? (maxWidth - len) / (cur - index) : (maxWidth - len);
String baseStr = genSpace(base);
int left = cur > index ? (maxWidth - len) % (cur - index) : 0;
String leftStr = genSpace(base + 1);
for(int i = index; i <= cur; i++){
sb.append(words[i]);
if(i < cur){
sb.append(left > 0 ? leftStr : baseStr);
left--;
}
}
}
if(sb.length() < maxWidth){
sb.append(genSpace(maxWidth - sb.length()));
}
ret.add(sb.toString());
index = cur + 1;
}
return ret;
} private String genSpace(int n){
char[] cs = new char[n];
Arrays.fill(cs, ' ');
return new String(cs);
}
}

Java实现 LeetCode 68 文本左右对齐的更多相关文章

  1. [leetcode] 68. 文本左右对齐(国区第240位AC的~)

    68. 文本左右对齐 国区第240位AC的~我还以为坑很多呢,一次过,嘿嘿,开心 其实很简单,注意题意:使用"贪心算法"来放置给定的单词:也就是说,尽可能多地往每行中放置单词. 也 ...

  2. Leetcode 68.文本左右对齐

    文本左右对齐 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本. 你应该使用"贪心算法"来放置给定的单 ...

  3. LeetCode(68):文本左右对齐

    Hard! 题目描述: 给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本. 你应该使用“贪心算法”来放置给定的单词:也就是 ...

  4. [LeetCode] Text Justification 文本左右对齐

    Given an array of words and a length L, format the text such that each line has exactly L characters ...

  5. html文本垂直居中对齐

    html文本垂直居中对齐,代码如下: <div id="box" style="height:100px; line-height:100px; border:1p ...

  6. CSS3 justify 文本两端对齐

    浏览器参照基准:Firefox4 and Later, Chrome5 and Later, Safari5 and Later, Opera10.53 and Later, IE5.5 and La ...

  7. Java for LeetCode 216 Combination Sum III

    Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...

  8. Java for LeetCode 214 Shortest Palindrome

    Given a string S, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  9. Java for LeetCode 212 Word Search II

    Given a 2D board and a list of words from the dictionary, find all words in the board. Each word mus ...

随机推荐

  1. [hdu1847]博弈,推理

    题意:一堆石子,有n个,两个人轮流取,每次都只能取2的幂次方个数,不能取的人输 思路:首先0是必败态,2的所有幂次都是必胜态.由于选的数模3只能是1或2,恰好又都是2的幂次,0,.3都为必败态,猜想3 ...

  2. [hdu1242]优先队列

    题意:给一个地图,'x'走一步代价为2,'.'走一步代价为1,求从s到t的最小代价.裸优先队列. #pragma comment(linker, "/STACK:10240000,10240 ...

  3. 使用jquery实现的自适应导航

    话不多说,直接晒代码 <div class="headering"> <div class="header-top"> <div ...

  4. resize允许你控制一个元素的可调整大小性

  5. Flutter RenderBox指南——绘制篇

    本文基于1.12.13+hotfix.8版本源码分析. 0.大纲 RenderBox的用法 通过RenderObjectWidget把RenderBox塞进界面 1.RenderBox 在flutte ...

  6. 当Tomcat遇上Netty

    故事背景 嘀嘀嘀~,生产事故,内存泄漏! 昨天下午,突然收到运维的消息,分部某系统生产环境内存泄漏了,帮忙排查一下. 排查过程 第一步,要日志 分部给到的异常日志大概是这样(鉴于公司规定禁止截图禁止拍 ...

  7. java中字符串截取

    1.使用StringUtils,需要导包 String strs = "abcdef1003535197"; System.out.println("=====2==== ...

  8. Liquibase使用小结

    简介 Liquibase是一个用于跟踪.管理和应用数据库变化的开源数据库重构工具.它将所有数据库的变化保存在XML文件中,便于版本控制和项目部署升级.在快速搭建项目的JHipster框架中集成了该工具 ...

  9. 当.Net成为大厂门槛代码小白该何去何从?

    掌握.Net已成为进入大厂的通行牌.越来越多的互联网软件公司开始使用.Net Core,根据去年数据显示腾讯.网易.顺丰.携程.中通.申通.同程艺龙.微医.233网校.问卷星.金蝶等关键业务已经在往. ...

  10. java方式实现归并排序

    一.基本思想 归并排序是建立在归并操作上的一种排序算法,该算法是采用分治法的一个典型应用.具体操作如下:所谓的分治就是分而治之,以一分为二的原则,先把序列平均分解成二个左右子序列,然后递归左右二个子序 ...