zoukankan      html  css  js  c++  java
  • 字符串句子专题

    字符串句子专题

    分为两类,一类有前置和后置空格;一类没有前置和后置空格。

    第一类:有前置和后置空格的模板

     1     s += " "; //这里在最后一个字符位置加上空格,这样最后一个字符串就不会遗漏
     2     string temp = "";  //临时字符串
     3     vector<string> vec; //存放字符串的数组
     4     for (char ch : s)  //遍历字符句子
     5     {
     6         if (ch == ' ') //遇到空格
     7         {
     8             if (!tmp.empty()) //临时字符串非空
     9             {
    10                 vec.push_back(tmp);
    11                 tmp.clear();  //清空临时字符串
    12             }
    13         }
    14         else
    15             tmp += ch; 
    16     }

    第二类:没有前置和后置空格的模板

     1 s += " ";
     2     string temp = "";
     3     vector<string> vec;
     4     for (char ch : s)
     5     {
     6         if (ch == ' ')
     7         {
     8             vec.push_back(tmp);
     9             tmp.clear();
    10         }
    11         else
    12             tmp += ch;
    13     }

     例题1592.重新排列单词间的空格

    题目:

    给你一个字符串 text ,该字符串由若干被空格包围的单词组成。每个单词由一个或者多个小写英文字母组成,并且两个单词之间至少存在一个空格。题目测试用例保证 text 至少包含一个单词 。

    请你重新排列空格,使每对相邻单词之间的空格数目都 相等 ,并尽可能 最大化 该数目。如果不能重新平均分配所有空格,请 将多余的空格放置在字符串末尾 ,这也意味着返回的字符串应当与原 text 字符串的长度相等。

    返回 重新排列空格后的字符串 。

    题目链接:https://leetcode-cn.com/problems/rearrange-spaces-between-words/

    题解:

    先把字符串的每个单词统计出来,用vector表示;然后用一个字符串把每个单词和空格间隔着连接起来,需要注意的是,空格的数量需要简单计算一下。

    代码:
     1 class Solution {
     2 public:
     3     string reorderSpaces(string text) {
     4         string tmp="";
     5         vector<string> vec;
     6         text+=" ";
     7         int cnt=-1;//因为上一行加了一个空格" ",所以cnt统计空格个数的时候,需要初始化为-1
     8         for(char ch : text)
     9         {
    10             if(!isalpha(ch))
    11             {
    12                 cnt++;
    13                 if(!tmp.empty())
    14                 {
    15                     vec.push_back(tmp);
    16                     tmp.clear();
    17                 }
    18             }
    19             else
    20             {
    21                 tmp+=ch;
    22             }
    23         }
    24         // int sum=0;
    25         int n = vec.size();
    26         text.clear();
    27         if(n==1)//如果不考虑一个单词的话,分母会出现0,会报错
    28         {
    29             text+=vec[0];
    30             text.insert(text.size(),cnt,' ');
    31             return text;
    32         }
    33 
    34         int qian = cnt/(n-1);//此处的分母是n-1,不是n,因为题目是针对两个相邻单词之间,只有n-1个位置
    35         int final = cnt-qian*(n-1);
    36         for(int i=0;i<n-1;i++)
    37         {
    38             text+=vec[i];
    39             text.insert(text.size(),qian,' ');
    40         }
    41         text+=vec.back();//连接最后一个字符串
    42         text.insert(text.size(),final,' ');
    43         return text;
    44     }
    45 };

    参考链接:https://leetcode-cn.com/problems/fan-zhuan-dan-ci-shun-xu-lcof/solution/yi-ge-mo-ban-shua-bian-suo-you-zi-fu-chu-x6vh/

    雪儿言
  • 相关阅读:
    《C# to IL》第一章 IL入门
    multiple users to one ec2 instance setup
    Route53 health check与 Cloudwatch alarm 没法绑定
    rsync aws ec2 pem
    通过jvm 查看死锁
    wait, notify 使用清晰讲解
    for aws associate exam
    docker 容器不能联网
    本地运行aws lambda credential 配置 (missing credential config error)
    Cannot connect to the Docker daemon. Is 'docker daemon' running on this host?
  • 原文地址:https://www.cnblogs.com/weixq351/p/15322058.html
Copyright © 2011-2022 走看看