算法第八周作业01
来源:互联网 发布:oracle数据库课程设计 编辑:程序博客网 时间:2024/06/01 21:32
Description
Given an array of words and a length L, format the text such that each line has exactly L 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 L 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.
For example,
words: [“This”, “is”, “an”, “example”, “of”, “text”, “justification.”]
L: 16.
Return the formatted lines as:
[ "This is an", "example of text", "justification. "]
Note: Each word is guaranteed not to exceed L in length.
Solution
- 迭代数组的每个字符串
- 利用计数器记录当前字符串长度和并与maxWidth比较
- 区分对待最后一行、一行中只有一个字符串、空数组输入等情况
- 对于间隔数不能整除maxWidth的情况下,前面的间隔数比后面的间隔数大1
Code
public List<String> fullJustify(String[] words, int maxWidth) { List<String> result = new ArrayList<>(); int counter = 0; int index = 0; // 迭代每个字符串 for (int i = index; i < words.length; i++) { // 判断当前字符(包括空格)是否超过maxWidth if (counter + words[i].length() + i - index > maxWidth) { // 如果当前字符串够了 result.add(appendStr(words, index, i - index, maxWidth - counter, false)); // 清空计数 counter = 0; index = i--; } else { // 计数字符串长度和 counter += words[i].length(); } } if (counter != 0 || result.size() == 0) { // 对于空字符串数组输入和最后一行的情况 result.add(appendStr(words, index, words.length - index, maxWidth - counter, true)); } return result; } // 拼接一行字符串 private String appendStr(String[] words, int index, int num, int spaces, boolean last) { StringBuilder sb = new StringBuilder(num * 2); if (num == 1) { // 对于只有一个字符串情况 return sb.append(words[index]).append(appendSpace(spaces)).toString(); } else if(last){ // 对于最后一行 for (int i = 0; i < num; i++) { sb.append(words[index + i]).append(" "); } return sb.append(appendSpace(spaces - num)).toString(); } // 对于普通情况,需要构建空格串间隔(前面有些间隔的空格串需要+1) String spaceStr = appendSpace(spaces / (num - 1)); for (int i = 0; i < num - 1; i++) { sb.append(words[index + i]).append(spaceStr); // 未能整除的前面的空格字符串长度要+1 if (i < spaces % (num - 1)) sb.append(" "); } return sb.append(words[index + num - 1]).toString(); } private String appendSpace(int spaces) { StringBuilder sb = new StringBuilder(spaces); for (int i = 0; i < spaces; i++) sb.append(" "); return sb.toString(); }
- 算法第八周作业01
- 算法作业_14(2017.4.12第八周)
- 算法作业_15(2017.4.15第八周)
- 算法课第八周作业 | Unique Paths
- 算法第八次作业
- 算法结构与设计基础作业第八周
- 算法分析与设计课程作业第八周#1
- C语言入门-第八周:作业01
- 第八周--作业要求
- 第八周作业
- 第八周作业
- 第八周作业1
- 第八周作业 1.1
- 第八周作业 1.3
- 第八周作业 2.0
- 第八周作业 3.0
- 第八周作业-1
- 第八周C++作业
- Python + C/C++ 嵌入式编程(1):多维数组Numpy.Array()在Python和C/C++文件间的传递问题
- C编程经验总结5(剧终)
- 从明天起
- java静态代理和动态代理
- effective java之类和接口
- 算法第八周作业01
- TCP/IP简介
- 利用Shell开发跳板机功能脚本案例
- 博客开通第一天
- 移动端rem布局
- 【c++】(初识)继承
- Buffered增强添加IO流功能
- Linux的五个查找命令
- Mybatis学习之读取配置文件(三)