leetcode 3606

3606: 优惠券校验器

复制代码
isalnum(ch)

ch 满足('A'<=ch<='Z') || ('a'<=ch<='z') || ('0'<=ch<='9')时返回真,否则返回假

复制代码
for(auto& group:groups){
    sort(group.begin(),group.end()); //每组内部排序
    ans.insert(ans.end(),group.begin(),group.end());
}
  • insert 的这段调用相当于"把当前组的所有元素整体尾插到 ans 后面"。

  • 4 组依次处理,最终 ans 里就是:electronics 的排序结果 → grocery 的排序结果 → pharmacy 的排序结果 → restaurant 的排序结果。

    class Solution {
    public:
    bool check(string code,bool isActive){
    for(char ch:code){
    if(ch!='_' && !isalnum(ch)) return false;
    }
    return isActive;
    }
    vector validateCoupons(vector& code, vector& businessLine, vector& isActive) {
    vector groups[4]; //长度为 4 的数组
    vector ans;
    for(int i=0;i<code.size();i++){
    if(!code[i].empty() && check(code[i],isActive[i])){
    if(businessLine[i]=="electronics") groups[0].push_back(code[i]);
    else if(businessLine[i]=="grocery") groups[1].push_back(code[i]);
    else if(businessLine[i]=="pharmacy") groups[2].push_back(code[i]);
    else if(businessLine[i]=="restaurant") groups[3].push_back(code[i]);
    }
    }
    for(auto& group:groups){
    sort(group.begin(),group.end()); //每组内部按标识符字典序排序
    ans.insert(ans.end(),group.begin(),group.end());
    }

    复制代码
          return ans;
      }

    };

相关推荐
倒头就睡的小比特13 小时前
算法竞赛C++常用的STL
c++·算法
小羊没烦恼!14 小时前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
猎头南楼15 小时前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
m0_5474866615 小时前
《数据结构教程》全套 PPT课件2026
数据结构
旖旎夜光16 小时前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark16 小时前
大规模并行计算中的负载均衡算法研究4
算法
Because_of_Her116 小时前
并查集-听课笔记
笔记·算法·并查集
码流子16 小时前
高速公路安全监测实践:碰撞监测预警+物联网底座,从感知到处置的闭环
大数据·人工智能·物联网·算法·架构
another heaven17 小时前
【算法/C++ MD5算法能否逆解码?原理、C++实现与同类哈希算法对比】
c++·算法·哈希算法
wzdark17 小时前
从算法设计模式看编程思维的抽象能力4
算法