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