String - 68. Text Justification
68. Text Justification
Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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 maxWidth 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.
Note:
- A word is defined as a character sequence consisting of non-space characters only.
- Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.
- The input array
words
contains at least one word.
Example 1:
Input: words = ["This", "is", "an", "example", "of", "text", "justification."] maxWidth = 16 Output: [ "This is an", "example of text", "justification. " ]
思路:
也是一个实现题,没有什么算法,就是找到每一行能放下的单词,然后按照case的规则拼接输出结果。
代码:
java:
public class Solution {
public List<String> fullJustify(String[] words, int maxWidth) {
List<String> lines = new ArrayList<String>();
int index = 0;
while (index < words.length) {
//count:该行所有单词累计总长度
int count = words[index].length();
//last:该行最后一个词的index
int last = index + 1;
while (last < words.length) {
//out of bound
if (words[last].length() + count + 1 > maxWidth) break;
//plus one for the space, if its a perfect fit it will fit
count += 1 + words[last].length();
last++;
}
StringBuilder builder = new StringBuilder();
//append该行第一个单词
builder.append(words[index]);
//这一行除去第一个已经append的单词,共剩下几个词语:diff 个:从index到last-1
int diff = last - index - 1;
// if last line or number of words in the line is 1, left-justified
//最后一行:每个单词中间一个空格, 剩余补上空白
if (last == words.length || diff == 0) {
for (int i = index+1; i < last; i++) {
builder.append(" ");
builder.append(words[i]);
}
for (int i = builder.length(); i < maxWidth; i++) {
builder.append(" ");
}
} else {
//不是最后一行:middle justified
//这一行总space的个数:(长度-累计单词总长度)
//每个单词后面space的个数:(长度-累计单词总长度)/单词个数
// r为需要平均分配到中间的空格总数
int spaces = (maxWidth - count) / diff;
int r = (maxWidth - count) % diff;
for (int i = index+1; i < last; i++) {
for(int k=spaces; k > 0; k--) {
builder.append(" ");
}
if(r > 0) {
builder.append(" ");
r--;
}
builder.append(" ");
builder.append(words[i]);
}
}
lines.add(builder.toString());
index = last;
}
return lines;
}
}
- 实用的前端开发小技巧汇集
- python编码问题
- 微信支付推出人脸识别智慧时尚试衣间,无感购物即将来袭
- 如何判断你买的域名有没有被K过?
- 【设计模式】Factory模式
- Windows:将cmd命令行添加到右键中方法
- 回家的低价票难抢?注意!可能是被“爬虫”吃了
- DeepMind团队回顾2017年:想象、推理取得突破
- flask-mail发送QQ邮件代码示例(亲测可行)
- 数据结构与算法C#版笔记--排序(Sort)-下
- pip --upgrade批量更新过期的python库
- 数据结构与算法C#版笔记--排序(Sort)-上
- android 模拟器安装二三事
- 2017小程序发展大事件和未来3大趋势分析
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Spring Boot 2.x基础教程:使用EhCache缓存集群
- 白平衡初探
- Activity的启动过程详解(基于10.0源码)
- Activity生命周期-你真的了解吗?
- Activity启动模式
- Redis6.0主从、哨兵、集群搭建和原理
- Linux入门学习笔记二
- 聊聊dubbo-go的tracingFilter
- 轻松学Pytorch –使用torchvision实现对象检测
- Linux core dump有什么用?
- Linux系统搭建C++开发环境
- Spring入门
- TKE容器实现限制用户在多个namespace上的访问权限(上)
- TKE容器实现限制用户在多个namespace上的访问权限(下)
- 小程序Android可以请求,ios请求失败wx.request