zoukankan      html  css  js  c++  java
  • 力扣算法题—068文本左右对齐

    给定一个单词数组和一个长度 maxWidth,重新排版单词,使其成为每行恰好有 maxWidth 个字符,且左右两端对齐的文本。

    你应该使用“贪心算法”来放置给定的单词;也就是说,尽可能多地往每行中放置单词。必要时可用空格 ' ' 填充,使得每行恰好有 maxWidth 个字符。

    要求尽可能均匀分配单词间的空格数量。如果某一行单词间的空格不能均匀分配,则左侧放置的空格数要多于右侧的空格数。

    文本的最后一行应为左对齐,且单词之间不插入额外的空格。

    说明:

    • 单词是指由非空格字符组成的字符序列。
    • 每个单词的长度大于 0,小于等于 maxWidth
    • 输入单词数组 words 至少包含一个单词。

    示例:

    输入:
    words = ["This", "is", "an", "example", "of", "text", "justification."]
    maxWidth = 16
    输出:
    [
       "This    is    an",
       "example  of text",
       "justification.  "
    ]
    

    示例 2:

    输入:
    words = ["What","must","be","acknowledgment","shall","be"]
    maxWidth = 16
    输出:
    [
      "What   must   be",
      "acknowledgment  ",
      "shall be        "
    ]
    解释: 注意最后一行的格式应为 "shall be    " 而不是 "shall     be",
         因为最后一行应为左对齐,而不是左右两端对齐。       
         第二行同样为左对齐,这是因为这行只包含一个单词。
    

    示例 3:

    输入:
    words = ["Science","is","what","we","understand","well","enough","to","explain",
             "to","a","computer.","Art","is","everything","else","we","do"]
    maxWidth = 20
    输出:
    [
      "Science  is  what we",
      "understand      well",
      "enough to explain to",
      "a  computer.  Art is",
      "everything  else  we",
      "do                  "
    ]
      1 #include "_000库函数.h"
      2 
      3 class Solution {
      4 public:
      5     vector<string> fullJustify(vector<string>& words, int maxWidth) {
      6         vector<string>res;
      7         vector<string>lin;//一行单词的存放量
      8         int n = 0;//一行字符的长度
      9         for (int i = 0; i < words.size(); ++i) {
     10             n += words[i].size();//单词长度
     11             lin.push_back(words[i]);
     12             if (n + lin.size() - 1 > maxWidth) {//长度超过了lin.size() - 1为需要加空格的长度
     13                 n -= words[i].size();
     14                 lin.pop_back();//最后一个单词放不进去
     15                 string str = "";
     16                 int num = (lin.size() - 1) > 0 ? (lin.size() - 1) : (maxWidth - n + 1);//用来防止只有一个单词的情况
     17                 int k = (maxWidth - n) % num;//如果无法均分,则左边的空格要更多
     18                 for (int j = 0; j < lin.size(); ++j) {
     19                     str += lin[j];
     20                     if (lin.size() == 1) {//只有一个单词
     21                         str.insert(str.end(), k, ' ');
     22                         break;
     23                     }
     24                     if (k - j > 0)str += " ";//左边的多加空格
     25                     if (j < lin.size() - 1)
     26                         str.insert(str.end(), (maxWidth - n) / num, ' ');
     27                 }
     28                 res.push_back(str);
     29                 --i;
     30                 n = 0;
     31                 lin.clear();
     32             }
     33         }
     34         if (!lin.empty()) {//最后一点单词了
     35             string str = "";
     36             for (int j = 0; j < lin.size(); ++j) {
     37                 str += lin[j];
     38                 if (j < lin.size() - 1)
     39                     str += " ";//最后一行为左对齐,单词间只需要添加一个空格就行
     40             }
     41             str.insert(str.end(), (maxWidth - n - lin.size() + 1), ' ');//最后拿剩余的空格顶替
     42             res.push_back(str);
     43         }
     44         return res;
     45     }
     46 };
     47 
     48 
     49 //方法二,更简洁,但跟方法一的思想一样
     50 class Solution {
     51 public:
     52     vector<string> fullJustify(vector<string> &words, int L) {
     53         vector<string> res;
     54         int i = 0;
     55         while (i < words.size()) {
     56             int j = i, len = 0;
     57             while (j < words.size() && len + words[j].size() + j - i <= L) {
     58                 len += words[j++].size();
     59             }
     60             string out;
     61             int space = L - len;
     62             for (int k = i; k < j; ++k) {
     63                 out += words[k];
     64                 if (space > 0) {
     65                     int tmp;
     66                     if (j == words.size()) {
     67                         if (j - k == 1) tmp = space;
     68                         else tmp = 1;
     69                     }
     70                     else {
     71                         if (j - k - 1 > 0) {
     72                             if (space % (j - k - 1) == 0) tmp = space / (j - k - 1);
     73                             else tmp = space / (j - k - 1) + 1;
     74                         }
     75                         else tmp = space;
     76                     }
     77                     out.append(tmp, ' ');
     78                     space -= tmp;
     79                 }
     80             }
     81             res.push_back(out);
     82             i = j;
     83         }
     84         return res;
     85     }
     86 };
     87 
     88 void T068() {
     89     Solution s;
     90     vector<string>v;
     91     v = { "aaaaaa","bbbbbb","This", "is", "an", "example", "of", "text", "justification." };
     92     v = s.fullJustify(v, 16);
     93     for (auto a : v)
     94         cout << a << endl;
     95     cout << endl;
     96     v = { "What","must","be","acknowledgment","shall","be"};
     97     v = s.fullJustify(v, 16);
     98     for (auto a : v)
     99         cout << a << endl;
    100     cout << endl;
    101     v = { "Science","is","what","we","understand","well","enough","to","explain",
    102          "to","a","computer.","Art","is","everything","else","we","do" };
    103     v = s.fullJustify(v, 20);
    104     for (auto a : v)
    105         cout << a << endl;
    106     cout << endl;
    107 
    108 }


  • 相关阅读:
    python web自动化测试框架搭建(功能&接口)——通用模块
    Python web自动化测试框架搭建(功能&接口)——unittest介绍
    什么是Referrer-Policy
    git pull 问题
    [坑]sed命令替换指定字符后不固定字符串,文本信息的替换删除
    修改Mysql procedure,function and view definer
    遵循PEP8风格指南
    Mysql create constraint foreign key faild.trouble shooting method share
    Spring JDBC FOUND_ROWS 安全吗?
    邮件安全产品方案
  • 原文地址:https://www.cnblogs.com/zzw1024/p/10696199.html
Copyright © 2011-2022 走看看