【leetcode】Text Justification

Text Justification

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 exactlyL 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.

Corner Cases:

  • A line other than the last line might contain only one word. What should you do in this case?
    In this case, that line should be left-justified.

1.每两个单词之间至少一个空格

2.相邻空格数目之差不超过1

3.在左边的空格数目一定不小于右边空格的数目

4.如果一行只有一个单词,则左对齐,后面插空格

5.对于最后一行,左对齐,两个单词之间必须只空一个空格

对于非最后一行,假设所有单词的总长度为totalWordsLen,则必须插入的空格书为spaceNum=L-totalWordsLen

设共有n+1个单词,则有n个间隔

则第i个间隔处的空格数space[i]可以通过下面计算

int n1=spaceNum/n;

int n2=spaceNum%n;

vector<int> space(n,n1);

for(int i=0;i<n2;i++)  space[i]+=1;

 1 class Solution {
 2 public:
 3     vector<string> fullJustify(vector<string> &words, int L) {
 4
 5         int n=words.size();
 6
 7         vector<string> result;
 8
 9         int count=0;
10         int totalWordsLen=0;
11         int startIndex=0;
12
13         string tmpStr;
14
15         for(int i=0;i<n-1;i++)
16         {
17             count++;
18             totalWordsLen+=words[i].size();
19             int len=totalWordsLen+count-1;
20
21             if(len+words[i+1].size()+1>L)
22             {
23                 tmpStr=formLine(startIndex,i,L,totalWordsLen,words);
24                 result.push_back(tmpStr);
25
26                 count=0;
27                 totalWordsLen=0;
28                 startIndex=i+1;
29             }
30         }
31
32         tmpStr=formLastLine(startIndex,L,words);
33         result.push_back(tmpStr);
34
35         return result;
36     }
37     string formLine(int start,int end,int &L,int totalWordsLen,vector<string> &words)
38     {
39         int n=end-start;
40         int spaceNum=L-totalWordsLen;
41         if(n==0) return words[start]+string(spaceNum,‘ ‘);
42
43         int n1=spaceNum/n;
44         int n2=spaceNum%n;
45
46         vector<int> space(n,n1);
47         for(int i=0;i<n2;i++)  space[i]+=1;
48
49         string tmpStr="";
50         for(int i=start;i<end;i++)
51         {
52             tmpStr+=(words[i]+string(space[i-start],‘ ‘));
53         }
54
55         tmpStr+=words[end];
56         return tmpStr;
57
58     }
59
60     string formLastLine(int start,int &L,vector<string> &words)
61     {
62         string tmpStr=words[start];
63         for(int i=start+1;i<words.size();i++)
64         {
65             tmpStr+=‘ ‘+words[i];
66         }
67         while(tmpStr.length()<L)
68         {
69             tmpStr+=‘ ‘;
70         }
71         return tmpStr;
72     }
73 };
74  
时间: 2024-10-29 19:07:54

【leetcode】Text Justification的相关文章

【leetcode】 Text Justification

问题: 给定一个字符串数组words,一个整数L,将words中的字符串按行编辑,L表示每行的长度. 要求: 1)每个单词之间至少是有一个空格隔开的. 2)最后一行每个单词间只间隔一个空格, 最后一个单词后不足L长度的用空格填充. 3)除最后一行外,其他行进行填充长度的空格要均分,不能均分的,将余数代表的空格数依次填充在行左. For example, words: ["This", "is", "an", "example"

【LeetCode】Text Justification 解题报告

[题目] 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. P

【leetcode】Text Justification(hard) ☆

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 ex

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman

【leetcode】Generate Parentheses

题目: 给定整数n,返回n对匹配的小括号字符串数组. For example, given n = 3, a solution set is: "((()))", "(()())", "(())()", "()(())", "()()()" 分析: 这种问题的模式是:1)问题的解有多个 ,2)每个解都是由多个有效的 "步骤" 组成的,3)变更以有解的某个或某些"步骤"

【LeetCode】Implement strStr()

Implement strStr() Implement strStr(). Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack. 标准KMP算法.可参考下文. http://blog.csdn.net/yaochunnian/article/details/7059486 核心思想在于求出模式串前缀与后缀中重复部分,将重复信息保存在n

【LeetCode】Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. Input: (2 -> 4 -> 3) + (5 -> 6 ->

【LeetCode】Pascal&#39;s Triangle

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] 这题别想用通项公式做,n choose m里面的连乘必然溢出,老老实实逐层用定义做. class Solution { public: vector<vector<

【LeetCode】Copy List with Random Pointer

A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list. 思路:第一遍正常复制链表,同时用哈希表保存链表中原始节点和新节点的对应关系,第二遍遍历链表的时候,再复制随机域. 这是一种典型的空间换时间的做法,n个节点,需要大小为O(n