【五月集训5.2】—字符串

请添加图片描述

☘前言☘

开更五月集训专题,由浅入深,深入浅出,飞向大厂!

🧑🏻作者简介:一个从工业设计改行学嵌入式的年轻人
✨联系方式:2201891280(QQ)
全文大约阅读时间: 20min



500. 键盘行

500. 键盘行

解题思路

写个hash表记录行数,如果有不是当前行的直接跳出就好了。

代码

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        vector<string> ans;
        char hash[] = {2,3,3,2,1,2,2,2,1,2,2,2,3,3,1,1,1,1,2,1,1,3,1,3,1,3};
        int n = words.size();
        for(int i = 0;i < n;++i){
            int size = words[i].size(),tmp = hash[words[i][0] - 'a' >= 0 ? words[i][0] - 'a' : words[i][0] - 'a' + ' '], j;
            for(j = 1;j < size;++j)
                if(hash[words[i][j] -'a' >= 0 ? words[i][j] -'a'  : words[i][j] -'a'  + ' '] != tmp)   break;
            if(j == size)   ans.push_back(words[i]);
        }
        return ans;
    }
};

注意的点

  1. 大小写只差一个 一定要记住,看不见的是空格0.0
  2. 可以不用增加变量,用变量j判断是否是中途跳出。

1160. 拼写单词

1160. 拼写单词

解题思路

简单的hash表应用,继续练习c++的stl用法0.0

代码

class Solution {
public:
    int countCharacters(vector<string>& words, string chars) {
        int ans = 0;
        int hash[26] = {0};
        int size = chars.size();
        for(auto chari : chars) ++hash[chari - 'a'];

        for(auto wordi : words){
            int j, sizetmp = wordi.size(),tmphash[26] = {0};
            for(j = 0;j < sizetmp;++j)
                if(++tmphash[wordi[j] - 'a'] > hash[wordi[j] - 'a'])    break;

            if(j == sizetmp)   ans += sizetmp;
        }
        return ans;
    }
};

注意的点

  1. 因为每次遍历都要一个临时hash,不如直接建立一个临时hash表

1047. 删除字符串中的所有相邻重复项

1047. 删除字符串中的所有相邻重复项

解题思路

直接用栈操作,与栈顶相同的时候直接出栈,不同的时候进栈。

代码

class Solution {
public:
    string removeDuplicates(string s) {
        string ans;
        for(auto ch : s)
            if(!ans.empty() && ans.back() == ch)
                ans.pop_back();
            else
                ans.push_back(ch);
        return ans;
    }
};

注意的点

  1. C++的string提供了类似栈的操作方式,所以直接进行相应的操作就行。

1935. 可以输入的最大单词数

1935. 可以输入的最大单词数

解题思路

我记得上次写我拆分了字符串,这好像完全不用拆分的,就直接判断就好了,不过要注意最后一个字符串的处理方式。

代码

class Solution {
public:
    int canBeTypedWords(string text, string brokenLetters) {
        bool hash[26] = {0};
        for(auto brokenLetter : brokenLetters)  hash[brokenLetter - 'a'] = true;
        
        int ans = 0, flag = true;
        for(auto ch : text){
            if(ch == ' '){
                if(flag)    ++ans;
                flag = true;
            }
            else if(hash[ch - 'a']) flag = false;
        }
        if(flag)    ++ans;
        return ans;
    }
};

注意的点

  1. C++的挺好的点就是懒得写类型就auto。。。
  2. 最后一个结束符其实直接到末尾了,所以需要判断再加1

写在最后

今天成功练习了string,学习进度+++++++++++++++++

  • 4
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

XingleiGao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值