feat: 修改232题中Java写法及命名

命名更易理解,写法更加简洁
This commit is contained in:
joeCarf
2021-12-17 15:03:39 +08:00
parent b137d79f89
commit e495a0fcce

View File

@ -130,43 +130,42 @@ Java
```java
class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2;
Stack<Integer> stackIn;
Stack<Integer> stackOut;
/** Initialize your data structure here. */
public MyQueue() {
stack1 = new Stack<>(); // 负责进栈
stack2 = new Stack<>(); // 负责出栈
stackIn = new Stack<>(); // 负责进栈
stackOut = new Stack<>(); // 负责出栈
}
/** Push element x to the back of queue. */
public void push(int x) {
stack1.push(x);
stackIn.push(x);
}
/** Removes the element from in front of queue and returns that element. */
public int pop() {
dumpStack1();
return stack2.pop();
dumpstackIn();
return stackOut.pop();
}
/** Get the front element. */
public int peek() {
dumpStack1();
return stack2.peek();
dumpstackIn();
return stackOut.peek();
}
/** Returns whether the queue is empty. */
public boolean empty() {
return stack1.isEmpty() && stack2.isEmpty();
return stackIn.isEmpty() && stackOut.isEmpty();
}
// 如果stack2为空那么将stack1中的元素全部放到stack2
private void dumpStack1(){
if (stack2.isEmpty()){
while (!stack1.isEmpty()){
stack2.push(stack1.pop());
}
// 如果stackOut为空那么将stackIn中的元素全部放到stackOut
private void dumpstackIn(){
if (!stackOut.isEmpty()) return;
while (!stackIn.isEmpty()){
stackOut.push(stackIn.pop());
}
}
}
@ -302,8 +301,8 @@ func (this *MyQueue) Empty() bool {
* Initialize your data structure here.
*/
var MyQueue = function() {
this.stack1 = [];
this.stack2 = [];
this.stackIn = [];
this.stackOut = [];
};
/**
@ -312,7 +311,7 @@ var MyQueue = function() {
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.stack1.push(x);
this.stackIn.push(x);
};
/**
@ -320,14 +319,14 @@ MyQueue.prototype.push = function(x) {
* @return {number}
*/
MyQueue.prototype.pop = function() {
const size = this.stack2.length;
const size = this.stackOut.length;
if(size) {
return this.stack2.pop();
return this.stackOut.pop();
}
while(this.stack1.length) {
this.stack2.push(this.stack1.pop());
while(this.stackIn.length) {
this.stackOut.push(this.stackIn.pop());
}
return this.stack2.pop();
return this.stackOut.pop();
};
/**
@ -336,7 +335,7 @@ MyQueue.prototype.pop = function() {
*/
MyQueue.prototype.peek = function() {
const x = this.pop();
this.stack2.push(x);
this.stackOut.push(x);
return x;
};
@ -345,7 +344,7 @@ MyQueue.prototype.peek = function() {
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return !this.stack1.length && !this.stack2.length
return !this.stackIn.length && !this.stackOut.length
};
```
@ -419,9 +418,9 @@ void myQueuePush(MyQueue* obj, int x) {
}
/*
1.若输出栈为空且当第一个栈中有元素stackInTop>0时将第一个栈中元素复制到第二个栈中stack2[stackTop2++] = stack1[--stackTop1])
1.若输出栈为空且当第一个栈中有元素stackInTop>0时将第一个栈中元素复制到第二个栈中stackOut[stackTop2++] = stackIn[--stackTop1])
2.将栈顶元素保存
3.当stackTop2>0时将第二个栈中元素复制到第一个栈中(stack1[stackTop1++] = stack2[--stackTop2])
3.当stackTop2>0时将第二个栈中元素复制到第一个栈中(stackIn[stackTop1++] = stackOut[--stackTop2])
*/
int myQueuePop(MyQueue* obj) {
//优化:复制栈顶指针,减少对内存的访问次数