LeetCode 题解(103): Text Justification
来源:互联网 发布:实战nginx 取代 编辑:程序博客网 时间:2024/06/04 17:49
题目:
Given an array of words and a length L, format the text such that each line has exactlyL 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.
题目:没有算法,暴力求解。
c++版:
class Solution {public:vector<string> fullJustify(vector<string>& words, int maxWidth) {vector<string> result;if (words.size() == 0 || maxWidth == 0) {result.push_back("");return result;}int i = 0;while (i < words.size()) {int end = i;int remain = maxWidth;string s = "";while (remain > 0 && remain - (int)words[end].length() >= 0) {//s += words[end];//s += " ";if (end != i) { if (remain - (int)words[end].length() == 0 && end + 1 != words.size()) break;remain -= 1;}remain -= words[end++].length();if (end >= words.size()) break;}if (remain < 0) {remain += (words[--end].length() + 1);//s = s.substr(0, s.find(words[end])-1);}if (end < words.size()) {if (end - i - 1 != 0) {int each = remain / (end - i - 1) + 1;int r = remain % (end - i - 1);for (int j = i; j < end; j++) {s.append(words[j]);if (j != end - 1) {for (int k = 0; k < each; k++)s.append(" ");if (j - i + 1 <= r)s.append(" ");}}}else {s.append(words[i]);for (int k = 0; k < remain; k++)s.append(" ");}}else {for (int j = i; j < end; j++) {s.append(words[j] + " ");}s = s.substr(0, s.length() - 1);for (int k = 0; k < remain; k++)s.append(" ");}result.push_back(s);i = end;}return result;}};
Java版;
public class Solution { public List<String> fullJustify(String[] words, int maxWidth) { List<String> result = new ArrayList<String>(); if(words.length == 0 || maxWidth == 0) { result.add(""); return result; } int i = 0; while(i < words.length) { int remain = maxWidth; int end = i; while(remain > 0 && remain - words[end].length() >= 0) { if(end != i) { if(remain - words[end].length() == 0 && end + 1 != words.length) break; remain--; } remain -= words[end++].length(); if(end == words.length) break; } if(remain < 0) { remain += (words[--end].length() + 1); } if(end < words.length) { if(end - i - 1 != 0) { int gap = remain / (end - i - 1) + 1; int r = remain % (end - i - 1); StringBuilder s = new StringBuilder(); for(int j = i; j < end; j++) { s.append(words[j]); if(j != end - 1) { for(int k = 0; k < gap; k++) s.append(" "); if(j - i + 1 <= r) s.append(" "); } } result.add(s.toString()); } else { StringBuilder s = new StringBuilder(); s.append(words[i]); for(int j = 0; j < remain; j++) s.append(" "); result.add(s.toString()); } } else { StringBuilder s = new StringBuilder(); for(int j = i; j < end; j++) { s.append(words[j]); s.append(" "); } s.delete(s.length()-1, s.length()); for(int k = 0; k < remain; k++) s.append(" "); result.add(s.toString()); } i = end; } return result; }}
Python版:
class Solution: # @param {string[]} words # @param {integer} maxWidth # @return {string[]} def fullJustify(self, words, maxWidth): result = [] if len(words) == 0 or maxWidth == 0: result.append("") return result i = 0 while i < len(words): end = i remain = maxWidth while remain > 0 and remain - len(words[end]) >= 0: if end != i: if remain - len(words[end]) == 0 and end + 1 == len(words): break; remain -= 1 remain -= len(words[end]) end += 1 if end == len(words): break if remain < 0: end -= 1 remain += (len(words[end]) + 1) if end != len(words): if end - i - 1 != 0: each = remain / (end - i - 1) + 1 r = remain % (end - i - 1) s = "" for j in range(i, end): s += words[j] if j != end - 1: for k in range(0, each): s += " " if j - i + 1 <= r: s += " " result.append(s) else: s = words[i] for j in range(0, remain): s += " " result.append(s) else: s = "" for j in range(i, end): s += words[j] s += " " s = s[:-1] for k in range(0, remain): s += " " result.append(s) i = end return result
- LeetCode 题解(103): Text Justification
- LeetCode - Text Justification 题解
- leetcode 103: Text Justification
- LeetCode(68) Text Justification
- LeetCode Text Justification(贪心)
- LeetCode 68. Text Justification(文本对齐)
- LeetCode刷题(36)--Text Justification
- LeetCode : Text Justification
- [LeetCode] Text Justification
- [LeetCode]Text Justification
- [leetcode] Text Justification
- Text Justification leetcode
- Leetcode: Text Justification
- leetcode Text Justification
- LeetCode Text Justification
- LeetCode | Text Justification
- LeetCode Text Justification
- [Leetcode] Text Justification (Java)
- POJ 1045 解题报告
- Android之UI主线程更新问题
- 强制清除Elasticsearch中已删除的文件
- maven打包编译时提示没有设定编码
- od的脚本学习
- LeetCode 题解(103): Text Justification
- 博客搬迁了
- 人需要有拼劲
- VB.Net中Socket异步编程的实例
- Android 布局(Layout)指南
- 五分钟一个设计模式之适配器模式
- 使用c#访问Access数据库时,提示找不到可安装的 ISAM
- NGINX轻松管理10万长连接 --- 基于2GB内存的CentOS 6.5 x86-64
- Java学习推荐书目