-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclear_digits.cpp
More file actions
62 lines (57 loc) · 1.29 KB
/
clear_digits.cpp
File metadata and controls
62 lines (57 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* =====================================================================================
*
* Filename: clear_digits.cpp
*
* Description: 3174. Clear Digits. https://leetcode.com/problems/clear-digits/
*
* Version: 1.0
* Created: 02/10/2025 13:29:28
* Revision: none
* Compiler: gcc
*
* Author: xianfeng.zhu@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <cctype>
#include <deque>
#include <string>
#include <utility>
#include <vector>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using std::deque;
using std::pair;
using std::string;
using std::vector;
class Solution {
public:
string clearDigits(string s) {
deque<char> q;
for (const char& c : s) {
if (std::isdigit(c) && q.size() > 0) {
q.pop_back();
} else {
q.push_back(c);
}
}
int i = 0;
s.resize(q.size());
while (!q.empty()) {
s[i++] = q.front();
q.pop_front();
}
return s;
}
};
TEST(Solution, clearDigits) {
vector<pair<string, string>> cases = {
std::make_pair("abc", "abc"),
std::make_pair("cb34", ""),
};
for (auto& c : cases) {
EXPECT_EQ(Solution().clearDigits(c.first), c.second);
}
}