(每日算法)leetcode--Text Justification(格式化字符串)

要求:

给定一组字符串,按照给定长度(eg.16)格式化显示,使每一行尽可能多的单词,单词之间的空格均衡-最左侧可以稍多,左右对齐。

最后一行靠左对齐,即最右侧可以没有间隙。

如下面的例子描述:

1)每一行16个字符

2)左右对齐,空格数均衡,最左侧的间隙可以多一个空格

3)单词不可以拆开,最后一行左对齐。

原题描述:

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.  "
]

代码如下:

vector<string> fullJustify(vector<string> &words, int L) {
     vector<string> result;
     const int n = words.size();
     int begin = 0, len = 0;//当前行的起点, 当前长度
     for(int i = 0; i < n; ++i)
     {
        if( len + words[i].size() + (i - begin) > L)
        {
            //如果加上第i个单词长度超过L,就作为一行添加到结果中
            //i-begin -- 指单词之间必须的一个空格数
            //先统计一行能容纳多少个单词,然后再真正的拼接
            //将words中begin到i-1的i-begin个单词拼接
            result.push_back(connect(words, begin, i-1, len, L, false));
            begin = i;
            len = 0;
        }
        len += words[i].size();//只是起统计这一行长度的作用
     }
     //将最后一行单独处理
     result.push_back(connect(words, begin, n-1, len, L, true));
     return result;
    }
//参数说明
//begin-开始拼接的索引         end-结束拼接的索引
//len-字母的总个数            is_last-是否是最后一行
string connect(vector<string> &words, int begin, int end,
                int len, int L, bool is_last)
{
    string s;
    int n = end - begin + 1;//要拼接的单词个数
    for(int i = 0; i < n; ++i)
    {
        s += words[begin + i];
        //添加一个新单词之后要添加空格
        addSpaces(s, i, n - 1, L - len, is_last);
    }
    //L--总共需要添加的空格数
    if(s.size() < L)
        s.append(L - s.size(), ' ');
    return s;
}
//参数说明
//i-当前空隙的序号    n-空隙总数
//L-总共需要添加的空隙数
void addSpaces(string &s, int i, int n, int L, bool is_last)
{
    if(n < 1 || i > n - 1) return;
    int spaces = is_last ? 1 : (L/n + (i < (L % n) ? 1 : 0));
    s.append(spaces, ' ');
}
时间: 2024-10-16 23:39:40

(每日算法)leetcode--Text Justification(格式化字符串)的相关文章

LeetCode: Text Justification [068]

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

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]Text Justification @ Python

原题地址:https://oj.leetcode.com/problems/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 approac

[LeetCode] Text Justification words显示的排序控制

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: 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 i

(每日算法)Leetcode -- Largest Rectangle in Histogram(最大实心矩形)

思路:如果时间复杂度要求是O(n 2 )的话,解法比较多也比较好理解.比如可以遍历,对于当前 i 位置上的立柱,计算出以这个i 立柱结尾的最大矩形,然后求出总的最大矩形. Given  n  non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

(每日算法)LeetCode --- Decode Ways

A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 Given an encoded message containing digits, determine the total number of ways to decode it. For example, Given encoded

(每日算法)LeetCode --- Remove Duplicates from Sorted Array II (删除重复元素II)

Remove Duplicates from Sorted Array II Leetcode 题目: Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, Given sorted array A = [1,1,1,2,2,3], Your function should return length = 5, and A is now [1,1,2,2

(每日算法)LeetCode --- Reverse Linked List II(旋转链表的指定部分)

Reverse Linked List II(旋转链表的指定部分) Leetcode Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n sati