当前位置:网站首页>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;
}
}
边栏推荐
- MATLAB被禁下一个会是LABVIEW吗?国产测试软件ATECLOUD崛起发力
- 9 common mistakes testers fall into
- Worthington's tried and tested cell isolation system protocols
- Navicat for mysql破解版安装
- exness: U.S. GDP shrinks, yen bounces back
- [Flutter] Flutter preloading of mixed development solves the problem of slow page loading for the first time
- Ubuntu中使用SQLite
- cp强制覆盖与不覆盖拷贝方法
- [MySQL series] MySQL database foundation
- vmtouch——Linux下的文件缓存管理神器
猜你喜欢
Minimum number to rotate array
Navicat如何连接MySQL
[email protected](using passwordYES)"/>
Navicat报错:1045-Access denied for user [email protected](using passwordYES)
How many ways does Selenium upload files?I don't believe you have me
LeetCode / Scala - 无重复字符最长子串 ,最长回文子串
Validation Framework-01
Recommendation systems: feature engineering, common features
2022-07-29:一共有n个人,从左到右排列,依次编号0~n-1, h[i]是第i个人的身高, v[i]是第i个人的分数, 要求从左到右选出一个子序列,在这个子序列中的人,从左到右身高是不下降的。
Navicat for mysql crack version installation
重新定义分析 - EventBridge 实时事件分析平台发布
随机推荐
3 tips for using hot events to create press releases?A must-see for self-media people
Toutiao We-Media Operation: How to Gain 500+ Fans in Toutiao Today?
Replace the executable file glibc version of the one
Self-study HarmonyOS application development (56) - Use Service to ensure that the application runs continuously in the background
【C Primer Plus第九章课后编程题】
会议OA之待开会议&&所有会议
Worthington Papain & Chymotrypsin & DNase I
字符串替换空格
MATLAB被禁下一个会是LABVIEW吗?国产测试软件ATECLOUD崛起发力
重新定义分析 - EventBridge 实时事件分析平台发布
旋转数组的最小数字
9 common mistakes testers fall into
专心致志做事情
My first understanding of MySql, and the basic syntax of DDL and DML and DQL in sql statements
Worthington Dissociation Enzymes: Collagenase and Four Basic Profiles
Internship in a group
【LeetCode每日一题】——872.叶子相似的树
X64 mfc140u.dll文件缺失->应用程序无法正常启动(0xc000007b)解决方法
谷歌浏览器(google)设置翻译中文,翻译选项不生效或没有弹出翻译选项
CMake Tutorial Tour(0)_Overview