Skip to content

Commit 6ffc83f

Browse files
committed
update: 添加问题“3606.优惠券校验器”的代码和题解 (#1222)
3606: AC.cpp - AC,59.14%,92.47% (#1221) + word(en) Signed-off-by: LetMeFly666 <[email protected]>
1 parent 26813c7 commit 6ffc83f

File tree

6 files changed

+267
-3
lines changed

6 files changed

+267
-3
lines changed

.commitmsg

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
3433: AC.py (#1219) + word(en)
2-
3-
py - AC,47.06%,91.18%
1+
3606: AC.cpp - AC,59.14%,92.47% (#1221) + word(en)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-13 22:26:53
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-13 22:42:38
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
// THIS CANNOT PASS
12+
class Solution {
13+
private:
14+
inline bool is_ok(string& s) {
15+
for (char c : s) {
16+
if (c != '_' && !isalnum(c)) {
17+
return false;
18+
}
19+
}
20+
return !s.empty();
21+
}
22+
public:
23+
vector<string> validateCoupons(vector<string>& code, vector<string>& businessLine, vector<bool>& isActive) {
24+
vector<string> electronics, grocery, pharmacy, restaurant;
25+
for (int i = 0; i < code.size(); i++) {
26+
if (!isActive[i]) {
27+
continue;
28+
}
29+
if (!is_ok(code[i])) {
30+
continue;
31+
}
32+
if (businessLine[i][0] == 'e') {
33+
electronics.push_back(code[i]);
34+
} else if (businessLine[i][0] == 'g') {
35+
grocery.push_back(code[i]);
36+
} else if (businessLine[i][0] == 'p') {
37+
pharmacy.push_back(code[i]);
38+
} else {
39+
restaurant.push_back(code[i]);
40+
}
41+
}
42+
sort(electronics.begin(), electronics.end());
43+
sort(grocery.begin(), grocery.end());
44+
sort(pharmacy.begin(), pharmacy.end());
45+
sort(restaurant.begin(), restaurant.end());
46+
vector<string> ans;
47+
ans.reserve(electronics.size() + grocery.size() + pharmacy.size() + restaurant.size());
48+
ans.insert(ans.end(), make_move_iterator(electronics.begin()), make_move_iterator(electronics.end()));
49+
ans.insert(ans.end(), make_move_iterator(grocery.begin()), make_move_iterator(grocery.end()));
50+
ans.insert(ans.end(), make_move_iterator(pharmacy.begin()), make_move_iterator(pharmacy.end()));
51+
ans.insert(ans.end(), make_move_iterator(restaurant.begin()), make_move_iterator(restaurant.end()));
52+
return ans;
53+
}
54+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* @Author: LetMeFly
3+
* @Date: 2025-12-13 22:41:43
4+
* @LastEditors: LetMeFly.xyz
5+
* @LastEditTime: 2025-12-13 22:42:29
6+
*/
7+
#if defined(_WIN32) || defined(__APPLE__)
8+
#include "_[1,2]toVector.h"
9+
#endif
10+
11+
class Solution {
12+
private:
13+
inline bool is_ok(string& s) {
14+
for (char c : s) {
15+
if (c != '_' && !isalnum(c)) {
16+
return false;
17+
}
18+
}
19+
return !s.empty();
20+
}
21+
public:
22+
vector<string> validateCoupons(vector<string>& code, vector<string>& businessLine, vector<bool>& isActive) {
23+
vector<string> electronics, grocery, pharmacy, restaurant;
24+
for (int i = 0; i < code.size(); i++) {
25+
if (!isActive[i]) {
26+
continue;
27+
}
28+
if (!is_ok(code[i])) {
29+
continue;
30+
}
31+
if (businessLine[i] == "electronics") {
32+
electronics.push_back(code[i]);
33+
} else if (businessLine[i] == "grocery") {
34+
grocery.push_back(code[i]);
35+
} else if (businessLine[i] == "pharmacy") {
36+
pharmacy.push_back(code[i]);
37+
} else if (businessLine[i] == "restaurant") {
38+
restaurant.push_back(code[i]);
39+
}
40+
}
41+
sort(electronics.begin(), electronics.end());
42+
sort(grocery.begin(), grocery.end());
43+
sort(pharmacy.begin(), pharmacy.end());
44+
sort(restaurant.begin(), restaurant.end());
45+
vector<string> ans;
46+
ans.reserve(electronics.size() + grocery.size() + pharmacy.size() + restaurant.size());
47+
ans.insert(ans.end(), make_move_iterator(electronics.begin()), make_move_iterator(electronics.end()));
48+
ans.insert(ans.end(), make_move_iterator(grocery.begin()), make_move_iterator(grocery.end()));
49+
ans.insert(ans.end(), make_move_iterator(pharmacy.begin()), make_move_iterator(pharmacy.end()));
50+
ans.insert(ans.end(), make_move_iterator(restaurant.begin()), make_move_iterator(restaurant.end()));
51+
return ans;
52+
}
53+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@
10571057
|3541.找到频率最高的元音和辅音|简单|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/09/13/LeetCode%203541.%E6%89%BE%E5%88%B0%E9%A2%91%E7%8E%87%E6%9C%80%E9%AB%98%E7%9A%84%E5%85%83%E9%9F%B3%E5%92%8C%E8%BE%85%E9%9F%B3/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/151653999" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/find-most-frequent-vowel-and-consonant/solutions/3780744/letmefly-3541zhao-dao-pin-lu-zui-gao-de-yv5vs/" target="_blank">LeetCode题解</a>|
10581058
|3577.统计计算机解锁顺序排列数|中等|<a href="https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/10/LeetCode%203577.%E7%BB%9F%E8%AE%A1%E8%AE%A1%E7%AE%97%E6%9C%BA%E8%A7%A3%E9%94%81%E9%A1%BA%E5%BA%8F%E6%8E%92%E5%88%97%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155791805" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-the-number-of-computer-unlocking-permutations/solutions/3854206/letmefly-3577tong-ji-ji-suan-ji-jie-suo-5b6b8/" target="_blank">LeetCode题解</a>|
10591059
|3583.统计特殊三元组|中等|<a href="https://leetcode.cn/problems/count-special-triplets/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/09/LeetCode%203583.%E7%BB%9F%E8%AE%A1%E7%89%B9%E6%AE%8A%E4%B8%89%E5%85%83%E7%BB%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155754955" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/count-special-triplets/solutions/3853381/letmefly-3583tong-ji-te-shu-san-yuan-zu-cdnx3/" target="_blank">LeetCode题解</a>|
1060+
|3606.优惠券校验器|简单|<a href="https://leetcode.cn/problems/coupon-code-validator/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2025/12/13/LeetCode%203606.%E4%BC%98%E6%83%A0%E5%88%B8%E6%A0%A1%E9%AA%8C%E5%99%A8/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/155893803" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/coupon-code-validator/solutions/3856383/letmefly-3606you-hui-quan-xiao-yan-qi-fe-vqir/" target="_blank">LeetCode题解</a>|
10601061
|剑指Offer0047.礼物的最大价值|简单|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/03/08/LeetCode%20%E5%89%91%E6%8C%87%20Offer%2047.%20%E7%A4%BC%E7%89%A9%E7%9A%84%E6%9C%80%E5%A4%A7%E4%BB%B7%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/129408765" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/li-wu-de-zui-da-jie-zhi-lcof/solutions/2155672/letmefly-jian-zhi-offer-47li-wu-de-zui-d-rekb/" target="_blank">LeetCode题解</a>|
10611062
|剑指OfferII0041.滑动窗口的平均值|简单|<a href="https://leetcode.cn/problems/qIsx9U/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/07/16/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200041.%20%E6%BB%91%E5%8A%A8%E7%AA%97%E5%8F%A3%E7%9A%84%E5%B9%B3%E5%9D%87%E5%80%BC/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125819216" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/qIsx9U/solution/by-tisfy-30mq/" target="_blank">LeetCode题解</a>|
10621063
|剑指OfferII0091.粉刷房子|中等|<a href="https://leetcode.cn/problems/JEj789/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/06/25/LeetCode%20%E5%89%91%E6%8C%87%20Offer%20II%200091.%20%E7%B2%89%E5%88%B7%E6%88%BF%E5%AD%90/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/125456885" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/JEj789/solution/letmefly-jian-zhi-offer-ii-091fen-shua-f-3olz/" target="_blank">LeetCode题解</a>|
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
title: 3606.优惠券校验器:分类 + 排序
3+
date: 2025-12-13 22:44:25
4+
tags: [题解, LeetCode, 简单, 数组, 字符串, 排序]
5+
categories: [题解, LeetCode]
6+
---
7+
8+
# 【LetMeFly】3606.优惠券校验器:分类 + 排序
9+
10+
力扣题目链接:[https://leetcode.cn/problems/coupon-code-validator/](https://leetcode.cn/problems/coupon-code-validator/)
11+
12+
<p>给你三个长度为 <code>n</code> 的数组,分别描述 <code>n</code> 个优惠券的属性:<code>code</code>、<code>businessLine</code> 和 <code>isActive</code>。其中,第 <code>i</code> 个优惠券具有以下属性:</p>
13+
14+
<ul>
15+
<li><code>code[i]</code>:一个 <strong>字符串</strong>,表示优惠券的标识符。</li>
16+
<li><code>businessLine[i]</code>:一个 <strong>字符串</strong>,表示优惠券所属的业务类别。</li>
17+
<li><code>isActive[i]</code>:一个 <strong>布尔值</strong>,表示优惠券是否当前有效。</li>
18+
</ul>
19+
20+
<p>当以下所有条件都满足时,优惠券被认为是&nbsp;<strong>有效的&nbsp;</strong>:</p>
21+
22+
<ol>
23+
<li><code>code[i]</code> 不能为空,并且仅由字母数字字符(a-z、A-Z、0-9)和下划线(<code>_</code>)组成。</li>
24+
<li><code>businessLine[i]</code> 必须是以下四个类别之一:<code>"electronics"</code>、<code>"grocery"</code>、<code>"pharmacy"</code>、<code>"restaurant"</code>。</li>
25+
<li><code>isActive[i]</code> 为 <strong>true&nbsp;</strong>。</li>
26+
</ol>
27+
28+
<p>返回所有&nbsp;<strong>有效优惠券的标识符&nbsp;</strong>组成的数组,按照以下规则排序:</p>
29+
30+
<ul>
31+
<li>先按照其 <strong>businessLine</strong> 的顺序排序:<code>"electronics"</code>、<code>"grocery"</code>、<code>"pharmacy"</code>、<code>"restaurant"</code>。</li>
32+
<li>在每个类别内,再按照 <strong>标识符的字典序(升序)</strong>排序。</li>
33+
</ul>
34+
35+
<p>&nbsp;</p>
36+
37+
<p><strong class="example">示例 1:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>输入:</strong> <span class="example-io">code = ["SAVE20","","PHARMA5","SAVE@20"], businessLine = ["restaurant","grocery","pharmacy","restaurant"], isActive = [true,true,true,true]</span></p>
41+
42+
<p><strong>输出:</strong> <span class="example-io">["PHARMA5","SAVE20"]</span></p>
43+
44+
<p><strong>解释:</strong></p>
45+
46+
<ul>
47+
<li>第一个优惠券有效。</li>
48+
<li>第二个优惠券的标识符为空(无效)。</li>
49+
<li>第三个优惠券有效。</li>
50+
<li>第四个优惠券的标识符包含特殊字符 <code>@</code>(无效)。</li>
51+
</ul>
52+
</div>
53+
54+
<p><strong class="example">示例 2:</strong></p>
55+
56+
<div class="example-block">
57+
<p><strong>输入:</strong> <span class="example-io">code = ["GROCERY15","ELECTRONICS_50","DISCOUNT10"], businessLine = ["grocery","electronics","invalid"], isActive = [false,true,true]</span></p>
58+
59+
<p><strong>输出:</strong> <span class="example-io">["ELECTRONICS_50"]</span></p>
60+
61+
<p><strong>解释:</strong></p>
62+
63+
<ul>
64+
<li>第一个优惠券无效,因为它未激活。</li>
65+
<li>第二个优惠券有效。</li>
66+
<li>第三个优惠券无效,因为其业务类别无效。</li>
67+
</ul>
68+
</div>
69+
70+
<p>&nbsp;</p>
71+
72+
<p><strong>提示:</strong></p>
73+
74+
<ul>
75+
<li><code>n == code.length == businessLine.length == isActive.length</code></li>
76+
<li><code>1 &lt;= n &lt;= 100</code></li>
77+
<li><code>0 &lt;= code[i].length, businessLine[i].length &lt;= 100</code></li>
78+
<li><code>code[i]</code> 和 <code>businessLine[i]</code> 由可打印的 ASCII 字符组成。</li>
79+
<li><code>isActive[i]</code> 的值为 <code>true</code> 或 <code>false</code>。</li>
80+
</ul>
81+
82+
83+
84+
## 解题方法:分组 + 排序
85+
86+
> 分组/分类似乎差不多,暂不深究。
87+
88+
使用4个数组,分别存放合法的4类优惠券。最后对4个数组分别按字符串字典序排序,合并为一个数组并返回。
89+
90+
如何判断一个优惠券是否合法?
91+
92+
1. active为true
93+
2. businessLine属于4类之一
94+
3. code不为空且只由数组字母下划线组成
95+
96+
注意,C++中合并vector时若被合并vector后续无需再使用,则可以使用make_move_iterator在内存上移动。
97+
98+
+ 时间复杂度$O(L\log n)$,其中$L$是合法code总字符数
99+
+ 空间复杂度:C++$O(L)$
100+
101+
### AC代码
102+
103+
#### C++
104+
105+
```cpp
106+
/*
107+
* @LastEditTime: 2025-12-13 22:42:29
108+
*/
109+
class Solution {
110+
private:
111+
inline bool is_ok(string& s) {
112+
for (char c : s) {
113+
if (c != '_' && !isalnum(c)) {
114+
return false;
115+
}
116+
}
117+
return !s.empty();
118+
}
119+
public:
120+
vector<string> validateCoupons(vector<string>& code, vector<string>& businessLine, vector<bool>& isActive) {
121+
vector<string> electronics, grocery, pharmacy, restaurant;
122+
for (int i = 0; i < code.size(); i++) {
123+
if (!isActive[i]) {
124+
continue;
125+
}
126+
if (!is_ok(code[i])) {
127+
continue;
128+
}
129+
if (businessLine[i] == "electronics") {
130+
electronics.push_back(code[i]);
131+
} else if (businessLine[i] == "grocery") {
132+
grocery.push_back(code[i]);
133+
} else if (businessLine[i] == "pharmacy") {
134+
pharmacy.push_back(code[i]);
135+
} else if (businessLine[i] == "restaurant") {
136+
restaurant.push_back(code[i]);
137+
}
138+
}
139+
sort(electronics.begin(), electronics.end());
140+
sort(grocery.begin(), grocery.end());
141+
sort(pharmacy.begin(), pharmacy.end());
142+
sort(restaurant.begin(), restaurant.end());
143+
vector<string> ans;
144+
ans.reserve(electronics.size() + grocery.size() + pharmacy.size() + restaurant.size());
145+
ans.insert(ans.end(), make_move_iterator(electronics.begin()), make_move_iterator(electronics.end()));
146+
ans.insert(ans.end(), make_move_iterator(grocery.begin()), make_move_iterator(grocery.end()));
147+
ans.insert(ans.end(), make_move_iterator(pharmacy.begin()), make_move_iterator(pharmacy.end()));
148+
ans.insert(ans.end(), make_move_iterator(restaurant.begin()), make_move_iterator(restaurant.end()));
149+
return ans;
150+
}
151+
};
152+
```
153+
154+
> 同步发文于[CSDN](https://letmefly.blog.csdn.net/article/details/155893803)和我的[个人博客](https://blog.letmefly.xyz/),原创不易,转载经作者同意后请附上[原文链接](https://blog.letmefly.xyz/2025/12/13/LeetCode%203606.%E4%BC%98%E6%83%A0%E5%88%B8%E6%A0%A1%E9%AA%8C%E5%99%A8/)哦~
155+
>
156+
> 千篇源码题解[已开源](https://github.com/LetMeFly666/LeetCode)

Solutions/Other-English-LearningNotes-SomeWords.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,6 +1664,8 @@ categories: [自用]
16641664
|||
16651665
|pebble|n. 鹅卵石<br/>v. 用石子扔,用卵石覆盖|
16661666
|dome|n. 穹顶,圆屋顶,穹顶建筑物|
1667+
|||
1668+
|mercantile|adj. 商业化,贸易的|
16671669

16681670
+ 这个web要是能设计得可以闭眼(完全不睁眼)键盘控制背单词就好了。
16691671
+ 也许可以加个AI用最近词编故事功能(返回接口中支持标注所使用单词高亮?)

0 commit comments

Comments
 (0)