Skip to content

Commit bbe9cdd

Browse files
author
xwk1246
committed
push newest
1 parent 18577b3 commit bbe9cdd

6 files changed

+347
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <string>
2+
#include <stack>
3+
#include <iostream>
4+
#include <algorithm>
5+
using namespace std;
6+
/*
7+
* @lc app=leetcode id=1047 lang=cpp
8+
*
9+
* [1047] Remove All Adjacent Duplicates In String
10+
*/
11+
12+
// @lc code=start
13+
class Solution {
14+
public:
15+
string removeDuplicates(string s) {
16+
stack<char>s1;
17+
string out;
18+
for (auto i : s) {
19+
if (!s1.empty() && s1.top() == i) {
20+
s1.pop();
21+
}
22+
else {
23+
s1.push(i);
24+
}
25+
}
26+
while (!s1.empty()) {
27+
out += s1.top();
28+
s1.pop();
29+
}
30+
reverse(out.begin(), out.end());
31+
return out;
32+
}
33+
};
34+
// @lc code=end
35+
int main() {
36+
Solution s1;
37+
cout << s1.removeDuplicates("abbaca");
38+
return 0;
39+
}
40+
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <vector>
2+
#include <cctype>
3+
#include <string>
4+
#include <stack>
5+
#include <iostream>
6+
using namespace std;
7+
/*
8+
* @lc app=leetcode id=150 lang=cpp
9+
*
10+
* [150] Evaluate Reverse Polish Notation
11+
*/
12+
13+
// @lc code=start
14+
class Solution {
15+
public:
16+
stack<int>s1;
17+
int evalRPN(vector<string>& tokens) {
18+
for (auto i : tokens) {
19+
if (i == "+") {
20+
int tmp = s1.top();
21+
s1.pop();
22+
int result = s1.top() + tmp;
23+
s1.pop();
24+
s1.push(result);
25+
}
26+
else if (i == "-") {
27+
int tmp = s1.top();
28+
s1.pop();
29+
int result = s1.top() - tmp;
30+
s1.pop();
31+
s1.push(result);
32+
}
33+
else if (i == "*") {
34+
int tmp = s1.top();
35+
s1.pop();
36+
int result = s1.top() * tmp;
37+
s1.pop();
38+
s1.push(result);
39+
}
40+
else if (i == "/") {
41+
int tmp = s1.top();
42+
s1.pop();
43+
int result = s1.top() / tmp;
44+
s1.pop();
45+
s1.push(result);
46+
}
47+
else {
48+
s1.push(stoi(i));
49+
}
50+
}
51+
return s1.top();
52+
}
53+
};
54+
// @lc code=end
55+
int main() {
56+
Solution s1;
57+
vector<string>input = { "10", "6","9","3", "+","-11","*","/","*","17","+","5","+" };
58+
cout << s1.evalRPN(input);
59+
return 0;
60+
}
61+

225.implement-stack-using-queues.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <queue>
2+
#include <iostream>
3+
using namespace std;
4+
/*
5+
* @lc app=leetcode id=225 lang=cpp
6+
*
7+
* [225] Implement Stack using Queues
8+
*/
9+
10+
// @lc code=start
11+
class MyStack {
12+
public:
13+
queue<int>q1;
14+
queue<int>q2;
15+
/** Initialize your data structure here. */
16+
17+
MyStack() {
18+
19+
}
20+
21+
/** Push element x onto stack. */
22+
void push(int x) {
23+
q1.push(x);
24+
}
25+
26+
/** Removes the element on top of the stack and returns that element. */
27+
int pop() {
28+
int tmp;
29+
while ((int)q1.size() > 1) {
30+
q2.push(q1.front());
31+
q1.pop();
32+
}
33+
tmp = q1.front();
34+
q1.pop();
35+
while (!q2.empty()) {
36+
q1.push(q2.front());
37+
q2.pop();
38+
}
39+
return tmp;
40+
}
41+
42+
/** Get the top element. */
43+
int top() {
44+
int tmp = pop();
45+
q1.push(tmp);
46+
return tmp;
47+
}
48+
49+
/** Returns whether the stack is empty. */
50+
bool empty() {
51+
if (!q1.empty() || !q2.empty())return false;
52+
return true;
53+
}
54+
};
55+
56+
/**
57+
* Your MyStack object will be instantiated and called as such:
58+
* MyStack* obj = new MyStack();
59+
* obj->push(x);
60+
* int param_2 = obj->pop();
61+
* int param_3 = obj->top();
62+
* bool param_4 = obj->empty();
63+
*/
64+
// @lc code=end
65+
int main() {
66+
MyStack s;
67+
s.push(1);
68+
s.push(2);
69+
cout << s.pop() << endl;
70+
cout << s.top() << endl;
71+
72+
return 0;
73+
}
74+

232.implement-queue-using-stacks.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#include <stack>
2+
using namespace std;
3+
/*
4+
* @lc app=leetcode id=232 lang=cpp
5+
*
6+
* [232] Implement Queue using Stacks
7+
*/
8+
9+
// @lc code=start
10+
class MyQueue {
11+
public:
12+
/** Initialize your data structure here. */
13+
stack<int>sIn;
14+
stack<int>sOut;
15+
MyQueue() {
16+
}
17+
/** Push element x to the back of queue. */
18+
void push(int x) {
19+
sIn.push(x);
20+
}
21+
22+
/** Removes the element from in front of queue and returns that element. */
23+
int pop() {
24+
int buf;
25+
if (sOut.empty()) {
26+
while (!sIn.empty()) {
27+
sOut.push(sIn.top());
28+
sIn.pop();
29+
}
30+
buf = sOut.top();
31+
sOut.pop();
32+
return buf;
33+
}
34+
else {
35+
buf = sOut.top();
36+
sOut.pop();
37+
return buf;
38+
}
39+
}
40+
41+
/** Get the front element. */
42+
int peek() {
43+
int buf;
44+
buf = pop();
45+
sOut.push(buf);
46+
return buf;
47+
}
48+
49+
/** Returns whether the queue is empty. */
50+
bool empty() {
51+
if (!sIn.empty() || !sOut.empty()) {
52+
return false;
53+
}
54+
return true;
55+
}
56+
};
57+
58+
/**
59+
* Your MyQueue object will be instantiated and called as such:
60+
* MyQueue* obj = new MyQueue();
61+
* obj->push(x);
62+
* int param_2 = obj->pop();
63+
* int param_3 = obj->peek();
64+
* bool param_4 = obj->empty();
65+
*/
66+
// @lc code=end
67+

239.sliding-window-maximum.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <vector>
2+
#include <deque>
3+
#include <iostream>
4+
using namespace std;
5+
/*
6+
* @lc app=leetcode id=239 lang=cpp
7+
*
8+
* [239] Sliding Window Maximum
9+
*/
10+
11+
// @lc code=start
12+
class myqueue {
13+
public:
14+
deque<int>data;
15+
int push(int num) {
16+
if (data.empty()) {
17+
data.push_back(num);
18+
return num;
19+
}
20+
while (!data.empty() && num > data.back()) {
21+
data.pop_back();
22+
}
23+
data.push_back(num);
24+
return num;
25+
}
26+
int pop(int num) {
27+
if (data.front() == num) {
28+
data.pop_front();
29+
}
30+
return num;
31+
}
32+
int max() {
33+
return data.front();
34+
}
35+
};
36+
class Solution {
37+
public:
38+
vector<int> maxSlidingWindow(vector<int>& nums, int k) {
39+
myqueue mq;
40+
vector<int> out;
41+
int front = 0;
42+
for (int i = 0; i < k; i++) {
43+
mq.push(nums[i]);
44+
}
45+
out.push_back(mq.max());
46+
for (int i = 0; i + k < nums.size(); i++) {
47+
mq.pop(nums[i]);
48+
mq.push(nums[i + k]);
49+
out.push_back(mq.max());
50+
}
51+
return out;
52+
}
53+
};
54+
// @lc code=end
55+
int main() {
56+
Solution S1;
57+
vector<int>nums = { 1,3,1,2,0,5 };
58+
int k = 3;
59+
auto tmp = S1.maxSlidingWindow(nums, k);
60+
for (auto i : tmp) {
61+
cout << i << endl;
62+
}
63+
return 0;
64+
}
65+

459.repeated-substring-pattern.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <string>
2+
#include <iostream>
3+
using namespace std;
4+
5+
/*
6+
* @lc app=leetcode id=459 lang=cpp
7+
*
8+
* [459] Repeated Substring Pattern
9+
*/
10+
11+
// @lc code=start
12+
class Solution {
13+
public:
14+
void buildNext(int* next, string s) {
15+
int j = 0;
16+
next[0] = 0;
17+
for (int i = 1; i < s.length(); i++) {
18+
while (j > 0 && s[i] != s[j]) {
19+
j = next[j - 1];
20+
}
21+
if (s[i] == s[j])j++;
22+
next[i] = j;
23+
}
24+
25+
}
26+
bool repeatedSubstringPattern(string s) {
27+
int next[s.length()];
28+
buildNext(next, s);
29+
if (next[s.length() - 1] && s.length() % (s.length() - next[s.length() - 1]) == 0)return true;
30+
return false;
31+
}
32+
};
33+
// @lc code=end
34+
35+
int main() {
36+
Solution S1;
37+
string s = "abac";
38+
cout << S1.repeatedSubstringPattern(s);
39+
return 0;
40+
}

0 commit comments

Comments
 (0)