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. vue 在main.js里使用vue实例

    可以用 Vue.prototype 比如 Vue.prototype.$indicator.close(); 关闭正在加载的动画

  2. 记一次Oracle分区表全局索引重建的过程

    1.查询数据库各个表空间利用率: SELECT Upper(F.TABLESPACE_NAME) "表空间名", D.TOT_GROOTTE_MB "表空间大小(M)&q ...

  3. Java openrasp学习记录(一)

    前言: 最近一直在做学校实验室安排的项目,太惨了,没多少时间学习新知识,不过rasp还是要挤挤时间学的,先从小例子的分析开始,了解rasp的基本设计思路,后面详细阅读openrasp的源码进行学习!欢 ...

  4. Redux:pre

    If you aren't familiar with state management libraries like Redux or MobX, don't use context. For ma ...

  5. 【K8S】基于Docker+K8S+GitLab/SVN+Jenkins+Harbor搭建持续集成交付环境(环境搭建篇)

    写在前面 最近在 K8S 1.18.2 版本的集群上搭建DevOps环境,期间遇到了各种坑.目前,搭建环境的过程中出现的各种坑均已被填平,特此记录,并分享给大家! 服务器规划 IP 主机名 节点 操作 ...

  6. js读取json

    Json字符串是: [{"n":"aaa","un":"aaa"},{"n":"yang& ...

  7. 王玉兰201771010128实验二 Java基本程序设计

    第一部分:理论知识学习部分:  (1)标识符:标识符由字母.下划线.美元符号和数字组成,且第一个符号不能为数字.Hello.$1234.程序名.www_123都是合法标识符.标识符可用作类名.变量名. ...

  8. 【1-n】区间覆盖 TOJ4168+BZOJ1192

    Xiao Ming is very interesting for array. He given a sorted positive integer array and an integer n. ...

  9. 【Java】【JVM】Sychronized底层加锁原理详解

    我们首先先看看JMM模型,话不多说,上图: JMM对应的8大原子操作: read(读取):从主内存读取数据 load(载入):将主内存读取到的数据写入工作内存 use(使用):从工作内存读取数据来计算 ...

  10. 【Java_SSM】(四)Eclipse中通过maven引入jar包

    这篇博文我们介绍一下如何通过eclipse配置setting并引入jar包 (1)eclipse:Window--Preferences--Maven--User Setting 全部完成后点Appl ...