Skip to content

Commit 4bc86fc

Browse files
committed
new
1 parent d9e3d33 commit 4bc86fc

10 files changed

+632
-0
lines changed
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/***
2+
设计一个具有getMin功能的栈
3+
***/
4+
5+
#include <cstdio>
6+
#include <stack>
7+
8+
using namespace std;
9+
10+
class StackWithMin {
11+
public:
12+
void push( int x );
13+
void pop();
14+
int top();
15+
bool empty();
16+
int getMin();
17+
18+
private:
19+
stack<int> stackData;
20+
stack<int> stackMin;
21+
};
22+
23+
void StackWithMin::push( int x ) {
24+
stackData.push( x );
25+
if( stackMin.empty() ) {
26+
stackMin.push( x );
27+
}
28+
else {
29+
if( stackMin.top() >= x ) {
30+
stackMin.push( x );
31+
}
32+
}
33+
}
34+
35+
void StackWithMin::pop() {
36+
if( stackData.empty() ) {
37+
printf( "the stack is empty!" );
38+
}
39+
else {
40+
int cur = stackData.top();
41+
stackData.pop();
42+
if( cur == stackMin.top() ) {
43+
stackMin.pop();
44+
}
45+
}
46+
}
47+
48+
int StackWithMin::getMin() {
49+
if( stackData.empty() ) {
50+
printf( "the stack is empty!" );
51+
}
52+
else {
53+
return stackMin.top();
54+
}
55+
}
56+
57+
int StackWithMin::top() {
58+
if( stackData.empty() ) {
59+
printf( "the stack is empty!" );
60+
}
61+
else {
62+
return stackData.top();
63+
}
64+
}
65+
66+
bool StackWithMin::empty() {
67+
return stackData.empty();
68+
}
69+
70+
int main() {
71+
StackWithMin s;
72+
73+
s.push( 3 );
74+
s.push( 4 );
75+
s.push( 5 );
76+
s.push( 1 );
77+
s.push( 2 );
78+
s.push( 1 );
79+
s.pop();
80+
s.pop();
81+
s.pop();
82+
83+
printf( "%d\n", s.getMin() );
84+
85+
return 0;
86+
}

1-2.两个栈实现队列.cpp

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <iostream>
2+
#include <stack>
3+
4+
#define ERROR -10000
5+
6+
using namespace std;
7+
8+
class myQueue {
9+
public:
10+
void add( int x );
11+
int poll();
12+
int peek();
13+
private:
14+
stack<int> s1;
15+
stack<int> s2;
16+
};
17+
18+
void myQueue::add( int x ) {
19+
s1.push( x );
20+
}
21+
22+
int myQueue::poll() {
23+
int res;
24+
res = peek();
25+
26+
if( res != ERROR ) {
27+
s2.pop();
28+
}
29+
30+
return res;
31+
}
32+
33+
int myQueue::peek() {
34+
int res;
35+
if( !s2.empty() ) {
36+
res = s2.top();
37+
}
38+
else {
39+
if( s1.empty() ) {
40+
printf( "the queue is empty!\n" );
41+
return ERROR;
42+
}
43+
else {
44+
while( !s1.empty() ) {
45+
int cur = s1.top();
46+
s1.pop();
47+
s2.push( cur );
48+
}
49+
res = s2.top();
50+
}
51+
}
52+
53+
return res;
54+
}
55+
56+
int main() {
57+
myQueue q;
58+
q.add( 1 );
59+
q.add( 2 );
60+
q.add( 3 );
61+
printf( "%d\n", q.peek() );
62+
q.poll();
63+
printf( "%d\n", q.peek() );
64+
q.poll();
65+
q.add( 5 );
66+
q.add( 6 );
67+
printf( "%d\n", q.peek() );
68+
q.poll();
69+
printf( "%d\n", q.peek() );
70+
q.poll();
71+
printf( "%d\n", q.peek() );
72+
73+
return 0;
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/***
2+
仅用递归函数和栈操作
3+
实现栈的逆序
4+
***/
5+
6+
#include <iostream>
7+
#include <stack>
8+
9+
using namespace std;
10+
11+
int getAndRemoveLastElement( stack<int> &s ) {
12+
int result = s.top();
13+
s.pop();
14+
15+
if( s.empty() ) {
16+
return result;
17+
}
18+
else {
19+
int last = getAndRemoveLastElement( s );
20+
s.push( result );
21+
return last;
22+
}
23+
}
24+
25+
void reverse( stack<int> &s ) {
26+
if( s.empty() ) {
27+
return;
28+
}
29+
else {
30+
int i = getAndRemoveLastElement( s );
31+
reverse( s );
32+
s.push( i );
33+
}
34+
}
35+
36+
int main() {
37+
stack<int> s;
38+
s.push( 1 );
39+
s.push( 2 );
40+
s.push( 3 );
41+
42+
reverse( s );
43+
44+
while( !s.empty() ) {
45+
printf( "%d\n", s.top() );
46+
s.pop();
47+
}
48+
49+
return 0;
50+
}
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <iostream>
2+
#include <stack>
3+
4+
using namespace std;
5+
6+
void stackSort( stack<int> &s ) {
7+
stack<int> help;
8+
while( !s.empty() ) {
9+
int cur = s.top();
10+
s.pop();
11+
while( !help.empty() && cur > help.top() ) {
12+
int top = help.top();
13+
help.pop();
14+
s.push( top );
15+
}
16+
help.push( cur );
17+
}
18+
19+
while( !help.empty() ) {
20+
s.push( help.top() );
21+
help.pop();
22+
}
23+
}
24+
25+
void printStack( stack<int> &s ) {
26+
while( !s.empty() ) {
27+
printf( "%d\n", s.top() );
28+
s.pop();
29+
}
30+
}
31+
32+
int main() {
33+
stack<int> stackData;
34+
35+
stackData.push( 3 );
36+
stackData.push( 2 );
37+
stackData.push( 1 );
38+
stackData.push( 5 );
39+
stackData.push( 4 );
40+
41+
stackSort( stackData );
42+
43+
printStack( stackData );
44+
45+
return 0;
46+
}
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
int process( int num, string left, string mid, string right, string from, string to ) {
7+
if( num == 1 ) {
8+
if( from == "mid" || to == "mid" ) {
9+
cout << "move 1 from " + from + " to " + to << endl;
10+
return 1;
11+
}
12+
else {
13+
cout << "move 1 from " + from + " to mid" << endl;
14+
cout << "move 1 from mid to " + to << endl;
15+
return 2;
16+
}
17+
}
18+
if( from == "mid" || to == "mid" ) {
19+
string another = ( from == "left" || to == "left" ) ? "right" : "left";
20+
int part1 = process( num - 1, left, mid, right, from, another );
21+
int part2 = 1;
22+
cout << "move ";
23+
cout << num;
24+
cout << " from " + from + " to " + to << endl;
25+
int part3 = process( num - 1, left, mid, right, another, to );
26+
return part1 + part2 + part3;
27+
}
28+
else {
29+
int part1 = process( num - 1, left, mid, right, from, to );
30+
int part2 = 1;
31+
cout << "move ";
32+
cout << num;
33+
cout << " from " + from + " to mid" << endl;
34+
int part3 = process( num - 1, left, mid, right, to, from );
35+
int part4 = 1;
36+
cout << "move ";
37+
cout << num;
38+
cout << " from mid to " + to << endl;
39+
int part5 = process( num - 1, left, mid, right, from, to );
40+
return part1 + part2 + part3 + part4 + part5;
41+
}
42+
}
43+
44+
int hanoiProblem1( int num ) {
45+
if( num < 1 ) return 0;
46+
return process( num, "left", "mid", "right", "left", "right" );
47+
}
48+
49+
int main() {
50+
int sum = hanoiProblem1( 2 );
51+
printf( "%d\n", sum );
52+
53+
return 0;
54+
}

1-7.生成窗口最大值数组.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#include <iostream>
2+
#include <deque>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
vector<int> getMaxWindow( vector<int> &v, int w ) {
8+
vector<int> ans;
9+
10+
if( v.empty() || w < 1 || v.size() < w ) {
11+
return ans;
12+
}
13+
14+
deque<int> q;
15+
for( int i = 0; i < v.size(); i++ ) {
16+
while( !q.empty() && v[q.back()] <= v[i] ) {
17+
q.pop_back();
18+
}
19+
q.push_back( i );
20+
if( q.front() == i - w ) {
21+
q.pop_front();
22+
}
23+
if( i >= w - 1 ) {
24+
ans.push_back( v[q.front()] );
25+
}
26+
}
27+
28+
return ans;
29+
}
30+
31+
int main() {
32+
vector<int> vec;
33+
vec.push_back( 4 );
34+
vec.push_back( 3 );
35+
vec.push_back( 5 );
36+
vec.push_back( 4 );
37+
vec.push_back( 3 );
38+
vec.push_back( 3 );
39+
vec.push_back( 6 );
40+
vec.push_back( 7 );
41+
42+
vector<int> ans = getMaxWindow( vec, 3 );
43+
44+
for( int i = 0; i < ans.size(); i++ ) {
45+
printf( "%d ", ans[i] );
46+
}
47+
48+
return 0;
49+
}

0 commit comments

Comments
 (0)