-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path225.java
More file actions
144 lines (122 loc) · 3.42 KB
/
225.java
File metadata and controls
144 lines (122 loc) · 3.42 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
__________________________________________________________________________________________________
sample 40 ms submission
class MyStack {
Queue<Integer> queue1;
Queue<Integer> queue2;
/** Initialize your data structure here. */
public MyStack() {
queue1 = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue1.add(x);
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
queue2 = new LinkedList<>();
int size = queue1.size();
for(int i=0 ; i<size-1; i++){
int val = queue1.poll();
queue2.add(val);
}
int value = queue1.poll();
queue1 = queue2;
return value;
}
/** Get the top element. */
public int top() {
queue2 = new LinkedList<>();
int size = queue1.size();
for(int i=0 ; i<size-1; i++){
int val = queue1.poll();
queue2.add(val);
}
int value = queue1.peek();
queue2.add(value);
queue1 = queue2;
return value;
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue1.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
__________________________________________________________________________________________________
sample 32636 kb submission
class MyStack {
int st[] = new int[30];
int p = 0;
/** Initialize your data structure here. */
public MyStack() {
}
/** Push element x onto stack. */
public void push(int x) {
st[p++] = x;
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return st[--p];
}
/** Get the top element. */
public int top() {
return st[p-1];
}
/** Returns whether the stack is empty. */
public boolean empty() {
return p==0;
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/
__________________________________________________________________________________________________
sample 41 ms submission
class MyStack {
Queue<Integer> queue;
/** Initialize your data structure here. */
public MyStack() {
queue = new LinkedList<>();
}
/** Push element x onto stack. */
public void push(int x) {
queue.offer(x);
int size = queue.size();
while(size > 1){
queue.offer(queue.poll());
size--;
}
}
/** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
}
/** Get the top element. */
public int top() {
return queue.peek();
}
/** Returns whether the stack is empty. */
public boolean empty() {
return queue.isEmpty();
}
}
/**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/