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. 基于R语言的航空公司客户价值分析

    分析航空公司现状 1.行业内竞争 民航的竞争除了三大航空公司之间的竞争之外,还将加入新崛起的各类小型航空公司.民营航空公司,甚至国外航空巨头.航空产品生产过剩,产品同质化特征愈加明显,于是航空公司从价 ...

  2. ssh chroot 设置

    目的 让特定的用户登录linux服务器后,对其操作权限进行限制: 不能使用任何方式杀掉服务器现有的进程 最好只能查看相关的目录和文件 最好只能运行特定的命令,比如cat.ls.tail等 场景模拟 一 ...

  3. C# Html转pdf文件

    using iTextSharp.text; using iTextSharp.text.pdf; using System; using System.Collections.Generic; us ...

  4. 接口测试/soapUI

    忙过了2019年的下半年终于在2020年快上线了,~鞭炮噼啪过~ 项目技术架构:XML请求数据 -> JAVA (转换)-> JOSN请求数据 项目使用工具:soapUI/Jmeter,m ...

  5. 小程序externalClasses介绍

    小程序externalClasses 1.介绍:我们在封装组件的时候,有时候需要对外暴露出class,可以由调用者来决定组件中一部分的样式,此时就需要使用它了 // components/dong/i ...

  6. python re 里面match 和search的区别

    re.match()从开头开始匹配string. re.search()从anywhere 来匹配string. 例子: >>> re.match("c", &q ...

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

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

  8. Jquery学习2---倒计时

    以下代码是mvc4.0代码,其功能是让页面上的数字3,变2,变1 然后跳转页面 @{ ViewBag.Title = "LoginOut"; } <html> < ...

  9. Spring JSR-250 注释

    Spring还使用基于 JSR-250 注释,它包括 @PostConstruct 注释 @PreDestroy 注释 @Resource 注释 @PostConstruct 和 @PreDestro ...

  10. 【Java】手把手理解CAS实现原理

    先来看看概念,[CAS] 全称“CompareAndSwap”,中文翻译即“比较并替换”. 定义:CAS操作包含三个操作数 —— 内存位置(V),期望值(A),和新值(B). 如果内存位置的值与期望值 ...