Text Justification 实现两端对齐功能

实现office word中的两端对齐功能。

只有个单词时,右边补齐空格。最后一行每个词间一个空格,整下的空格右边补齐。给定字符串,和每行的字符数L。进行两端对齐输出。

我的思路是写一个函数,给定相应的参数就返回该行的string。然后在主函数里只要负责给参数就好了。参数包括words字符串数组本身,然后每个字符串的长度数组。开始start记录从字符串数组只的那个词开始,end记录到哪个词结束。L就是每行的最大字符数。在子函数里实现如果是最后一个单词了,那么就每个词空一个空格。其余后面补齐。在主函数里通过记录start到目前的长度是否等于或者超过L来判断start和end。如果end超过了长度就结束,返回结果即可。代码有点复杂。将近100行。调了9次bug终究AC了。

class Solution {
public:
// 给定相应的参数,返回该行字符串满足右对齐
string fun68(vector<string> &words, int len[], int start, int end, int L)
{
    if(start == end)
    {
        string subans = words[start];
        for (int i = 0; i < L - len[start]; ++i)
            subans += ‘ ‘;
        return subans;
    }
    string ans = "";
    int tolen = 0, inter = 0; // 词的长度tolen,两个词之间的空格inter
    for (int i = start; i <= end; i++)
    {
        tolen += len[i];
    }
    int left = L - tolen;
    inter = left / (end - start);
    int times = left - inter * (end - start);//剩times个空格需要在前times个词间加一个空格
    if(end == words.size() -1) // 如果是最后一个了,那么每个单词空一格单词,剩下的空格放在最后边
    {
        for (int i = start; i < words.size() - 1; i++)
        {
            ans += words[i] + ‘ ‘;
        }
        ans += words[end];
        left -= (end - start);
        for (int i = 0; i < left; i++)
            ans += ‘ ‘;
    }
    else // 否则就是平均分空格
    {
        for (int i = start; i <start + times; ++i)
        {
            ans += words[i];
            for (int j = 0; j < inter + 1; ++j)
            {
                ans += " ";
            }
        }
        for (int i = start + times; i < end; ++i) // 多出来的不能平均分的空格从左边开始分
        {
            ans += words[i];
            for (int j = 0; j < inter; ++j)
            {
                ans += " ";
            }
        }
        ans += words[end];
    }
    return ans;
}
//主函数
vector<string> fullJustify(vector<string> &words, int L)
{
    vector<string> ans;
    if (words.size() == 0)
    {
        return ans;
    }
    int n = words.size();
    int *len = new int[n];
    for (int i = 0; i < n; ++i)
    {
        len[i] = words[i].size();
    }
    int sum1 = len[0], sum2 = 0, start = 0, end = 0; // sum1记录从start开始的词到end的包括空格的长度
    string tmp = "";
    while (1)
    {
        if (sum1 == L || sum1 + 1 == L) // 如果有满足刚好等于L,那么就是要输出一行了
        {
            ans.push_back(fun68(words, len, start, end, L));
            start = end + 1;
            end = end + 1;
            if(end < n)// 没输出一行要相应的该sum的值,相当于重新计算过
            {
                sum1 = len[end];
                sum2 = 0;
                continue;
            }
            else
                break;
        }
        else if (end + 1 < n)
            sum2 = sum1 + 1 + len[end + 1];
        else
        {
            ans.push_back(fun68(words, len, start, end, L));
            break;
        }

        if (sum1 < L && sum2 > L) // 说明到end应结束一行
        {
            ans.push_back(fun68(words, len, start, end, L));
            start = end + 1;
            end = start;
            sum1 = len[end];
            sum2 = 0;
            continue;
        }
        sum1 += 1 + len[end + 1];
        end++;
    }
    return ans;
}
};

原创,但不是最好的。有时间了再学习学习50行的。

时间: 2024-08-05 06:33:31

Text Justification 实现两端对齐功能的相关文章

Text Justification,文本对齐

问题描述:把一个集合的单词按照每行L个字符放,每行要两端对齐,如果空格不能均匀分布在所有间隔中,那么左边的空格要多于右边的空格,最后一行靠左对齐. words: ["This", "is", "an", "example", "of", "text", "justification."]L: 16. Return the formatted lines as: [ &

[Swift]LeetCode68. 文本左右对齐 | Text Justification

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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

[leetcode]68. Text Justification文字对齐

Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth 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

怎样实现完美两端对齐/齐头尾功能——hyphenjs

因为之前代码写得太乱了.最近在重写,忘了开分支.有兴趣查看之前代码的朋友可以重置回c8034eb这个commit之前的代码看.重写完成后会重写一篇文章,抱歉啦. 前言 话不多说,先上图.后者红框里是浏览器默认的文本排版,右侧会有锯齿(至于难不难看就见仁见智啦哈哈).前者是使用自己开发的hyphenjs后的文本排版,整齐得像一块豆腐块!对于一个处女座来说,简直舒心了很多.去看看这个神奇的hyphenjs 应用场景 不会无缘无故的造轮子.在日常工作需求中,设计师丢给你一个PSD,发现里面的文本是两端

LeetCode --- 68. 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

No.68 Text Justification

No.68 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

【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

自适应的两端对齐:text-align:justify

1 2 3 <!DOCTYPE HTML> 4 <html> 5 <head> 6 <title>文本两端对齐 by hongchenok</title> 7 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 8 <style> 9 .box{ 10 width:50%; 11 padding:20

【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