Merge pull request #23 from coderhare/main

Lc 255, 1047更新cpp代码
This commit is contained in:
算法基地 2021-05-06 13:40:05 +08:00 committed by GitHub
commit 3081abf298
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 57 additions and 0 deletions

View File

@ -18,6 +18,8 @@
下面我们来看一下题目代码也是很容易理解
#### 题目代码
Java Code:
```java
class MyStack {
@ -87,3 +89,32 @@ MyStack.prototype.empty = function() {
};
```
C++ Code:
```cpp
class MyStack {
queue <int> q;
public:
void push(int x) {
q.push(x);
for(int i = 1;i < q.size();i++){
int val = q.front();
q.push(val);
q.pop();
}
}
int pop() {
int val = q.front();
q.pop();
return val;
}
int top() {
return q.front();
}
bool empty() {
return q.empty();
}
};
```

View File

@ -29,9 +29,11 @@
**题目代码**
Java Code:
```java
class Solution {
@ -67,3 +69,27 @@ class Solution {
```
当然这个题目也可以用 set 来做大家可以随意发挥
C++ Code:
```cpp
class Solution {
public:
string removeDuplicates(string S) {
string str;
if (S.empty() || S.size() == 1) {
return S;
}
for (int i = 0; i<S.size(); i++) {
if(str.empty() || S[i] != str.back()) {
str.push_back(S[i]);
}
else {
str.pop_back();
}
}
return str;
}
};
```