当前位置:网站首页>Leetcode68. 文本左右对齐
Leetcode68. 文本左右对齐
2022-07-30 01:02:00 【Java全栈研发大联盟】
题目传送地址:https://leetcode.cn/problems/text-justification/
运行效率:
代码如下:
class Solution {
public static List<String> fullJustify(String[] words, int maxWidth) {
List<String> res = new ArrayList<>();
StringBuilder row = new StringBuilder();
for (int i = 0; i < words.length; i++) {
String word = words[i];
if (row.length() == 0) {
row.append(word);
continue;
}
if (row.length() + word.length() + 1 <= maxWidth) {
//因为单词与单词之间需要加一个空格,所以这里要=1
row.append(" ").append(word);
} else {
res.add(row.toString());
row = new StringBuilder();
row.append(word);
}
}
if (!"".equals(row.toString())) {
res.add(row.toString());
}
List<String> result = fillList(res, maxWidth);
return result;
}
public static List<String> fillList(List<String> list, int maxWidth) {
List<String> res = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
//如果是最后一行 文本的最后一行应为左对齐,且单词之间不插入额外的空格。
if (i == list.size() - 1) {
int fillBlankNum = maxWidth - str.length();
StringBuilder stringBuilder = new StringBuilder(str);
while (fillBlankNum > 0) {
stringBuilder.append(" ");
fillBlankNum--;
}
res.add(stringBuilder.toString());
break;
}
int letterNum = getLetterNum(str);
//需要填充的空格数
int fillBlankNum = maxWidth - letterNum;
String[] strArray = str.split(" ");
//该行只有一个单词
if (strArray.length == 1) {
StringBuilder stringBuilder = new StringBuilder(strArray[0]);
while (fillBlankNum > 0) {
stringBuilder.append(" ");
fillBlankNum--;
}
res.add(stringBuilder.toString());
continue;
}
int avgBlankNum = fillBlankNum / (strArray.length - 1);//平均每两个单词之间应该保留的空格数
StringBuilder blank = new StringBuilder();
while (avgBlankNum > 0) {
blank.append(" ");
avgBlankNum--;
}
int moreBlankNum = fillBlankNum % (strArray.length - 1);//多出来的空格数
String join = String.join(blank, strArray);
StringBuilder stringBuilder = new StringBuilder(join);
int fromIndex = -1;
while (moreBlankNum > 0) {
int index = stringBuilder.indexOf(blank.toString(), fromIndex);
stringBuilder.insert(index, " ");
moreBlankNum--;
fromIndex =index+blank.length()+1;
}
res.add(stringBuilder.toString());
}
return res;
}
public static int getLetterNum(String str) {
int count = 0;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) != ' ') {
count++;
}
}
return count;
}
}
边栏推荐
- Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
- Linux - install MySQL (detailed tutorial)
- Vmtouch - under Linux file cache management artifact
- 小白必看|不用编程的labview,ATECLOUD完全满足你的需求
- npm ERR! code ENOTSUP npm ERR! notsup Unsupported engine for [email protected]: wanted: {“n
- 不要急,没有一朵花,从一开始就是花,也不要嚣张,没有一朵花,
- Replace the executable file glibc version of the one
- Navicat如何连接MySQL
- 1592. 重新排列单词间的空格
- [Flutter] Flutter preloading of mixed development solves the problem of slow page loading for the first time
猜你喜欢

How to increase account weight?3 ways to operate your own media to help you get more revenue

Linux - install MySQL (detailed tutorial)

STM32——OLED显示实验

旋转数组的最小数字

百度智能云章淼:详解企业级七层负载均衡开源软件BFE

【MySQL系列】MySQL数据库基础

Based on TNEWS 'today's headline news in Chinese short text classification

go语言解决自定义header的跨域问题

Replace the executable file glibc version of the one

Superficial understanding of CNN
随机推荐
Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
自学HarmonyOS应用开发(53)- 获取当前位置
Worthington Optimized Technology: Cell Quantification
Toutiao We-Media Operation: How to Gain 500+ Fans in Toutiao Today?
机器人的运动范围
专心致志做事情
Fabric 私有数据案例
Selenium上传文件有多少种方式?不信你有我全
旋转数组的最小数字
重新定义分析 - EventBridge 实时事件分析平台发布
How Navicat Connects to MySQL
【mysql】Mysql公用表表达式with as
cp强制覆盖与不覆盖拷贝方法
二叉排序树(C语言)
CMake Tutorial Tour(0)_Overview
7.28
He used to cells harvested trypsin & release procedure
nacos集群配置详解
exness:美国GDP萎缩,日元反弹受捧
泰克Tektronix示波器软件TDS2012|TDS2014|TDS2022上位机软件NS-Scope