Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 7 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Oct 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
增加 0225.用队列实现栈.md java 版本
优化,使用一个 Queue 实现
对比 『使用一个 Deque 实现』 的解法,我把移动元素的代码放到 push 方法里,代码看起来更简洁
  • Loading branch information
Flow-sandyu authored Oct 5, 2022
commit 680cf3dbd74d4266d1a077c493d0c440fd6bebfb
34 changes: 34 additions & 0 deletions problems/0225.用队列实现栈.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,40 @@ class MyStack {
}
```

优化,使用一个 Queue 实现
```java
class MyStack {

Queue<Integer> queue;

public MyStack() {
queue = new LinkedList<>();
}

//每 offer 一个数(A)进来,都重新排列,把这个数(A)放到队列的队首
public void push(int x) {
queue.offer(x);
int size = queue.size();
//移动除了 A 的其它数
while (size-- > 1)
queue.offer(queue.poll());
}

public int pop() {
return queue.poll();
}

public int top() {
return queue.peek();
}

public boolean empty() {
return queue.isEmpty();
}
}

```

Python:

```python
Expand Down

AltStyle によって変換されたページ (->オリジナル) /