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. 使用IR2104S搭建的H桥-机器人队比赛经典版(原作者答疑)

    原理图地址:http://bbs.ednchina.com/BLOG_ARTICLE_3020313.HTM?click_from=8800020962,4950449047,2014-05-01,E ...

  2. [hdu4497]分解质因数

    题意:求满足gcd(x,y,z)=G,lcm(x,y,z)=L的x,y,z的解的个数. 大致思路:首先如果L % G != 0那么无解,否则令u = L / G,问题变为,gcd(r,s,t)=1,l ...

  3. 豹子安全-注入工具-显错注入-asp_POST_显错_SQLServer_GetWebShell

    豹子安全-注入工具-显错注入-SQL Server-上传WebShell 请看下列视频 . 该视频在停留10秒钟后开始. 或点击此处查看:豹子安全-注入工具-asp_POST_显错_SQLServer ...

  4. ipad4密码忘记锁定了如何破解

    ipad4更新后被要求输入密码,但很长一段时间后忘记了,一直想不起来,也没有忘记密码的选项,以下是简单的破解方法. 注意:没有备份的资料是要被清空的 一.windows10系统,安装iTunes安装 ...

  5. linux 被入侵后扫尾工作

    比较恶心,安装了后门,还把ssh sshd 给改了,服务器在机房,还不大好处理,如果贸然上去改,还容易把自己关在外面. 但是我有salt ,上面可以用cmd.run运行一切命令.不走sshd服务. l ...

  6. 「雕爷学编程」Arduino动手做(40)——旋转编码器模块

    37款传感器与模块的提法,在网络上广泛流传,其实Arduino能够兼容的传感器模块肯定是不止37种的.鉴于本人手头积累了一些传感器和模块,依照实践出真知(一定要动手做)的理念,以学习和交流为目的,这里 ...

  7. Java基础之数据类型

    一.数据类型 基本数据类型介绍 byte 1字节 char 2字节 short 2字节 int 4字节 long 8字节 float 4字节 double 8字节 以上有Java中八大基本类型的7种, ...

  8. 解惑求助-关于NetCore2.2中间件响应的问题

    背景介绍:基于netcore2.2开发api接口程序,自定义了一个异常捕获中间件,用于捕获未经处理的异常以及状态码404.500等访问(设计的出发点就是,出现了非200的响应,我这边全部会进行处理成2 ...

  9. DPDK开发环境搭建(学会了步骤适合各版本)

    一.版本的选择 首先要说明的是,对于生产来说DPDK版本不是越高越好,如何选择合适的版本? 1.要选择长期支持的版本LTS(Long Term Support) 2.根据当前开发的系统环境选择 可以在 ...

  10. SpringBoot教程——检视阅读

    SpringBoot教程--检视阅读 参考 SpringBoot教程--一点--蓝本--springboot2.1.1 SpringBoot教程--易百--springboo2.0.5.RELEASE ...