Skip to content

Commit 3081abf

Browse files
authored
Merge pull request chefyuan#23 from coderhare/main
Lc 255, 1047更新cpp代码
2 parents 6b59314 + 411ddc3 commit 3081abf

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

animation-simulation/栈和队列/225.用队列实现栈.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
下面我们来看一下题目代码,也是很容易理解。
2020

21+
#### 题目代码
22+
2123
Java Code:
2224
```java
2325
class MyStack {
@@ -87,3 +89,32 @@ MyStack.prototype.empty = function() {
8789
};
8890
```
8991

92+
C++ Code:
93+
94+
```cpp
95+
class MyStack {
96+
queue <int> q;
97+
public:
98+
void push(int x) {
99+
q.push(x);
100+
for(int i = 1;i < q.size();i++){
101+
int val = q.front();
102+
q.push(val);
103+
q.pop();
104+
}
105+
}
106+
107+
int pop() {
108+
int val = q.front();
109+
q.pop();
110+
return val;
111+
}
112+
int top() {
113+
return q.front();
114+
}
115+
bool empty() {
116+
return q.empty();
117+
}
118+
};
119+
```
120+

animation-simulation/栈和队列/leetcode1047 删除字符串中的所有相邻重复项.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@
2929

3030

3131

32+
**题目代码**
3233

3334

3435

36+
Java Code:
3537

3638
```java
3739
class Solution {
@@ -67,3 +69,27 @@ class Solution {
6769
```
6870

6971
当然这个题目也可以用 set 来做,大家可以随意发挥
72+
73+
C++ Code:
74+
75+
```cpp
76+
class Solution {
77+
public:
78+
string removeDuplicates(string S) {
79+
string str;
80+
if (S.empty() || S.size() == 1) {
81+
return S;
82+
}
83+
for (int i = 0; i<S.size(); i++) {
84+
if(str.empty() || S[i] != str.back()) {
85+
str.push_back(S[i]);
86+
}
87+
else {
88+
str.pop_back();
89+
}
90+
}
91+
return str;
92+
}
93+
};
94+
```
95+

0 commit comments

Comments
 (0)