Skip to content

Commit 44d3573

Browse files
committed
提交作业
1 parent 9d0eb5e commit 44d3573

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

225.用队列实现栈.java

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
/*
5+
* @lc app=leetcode.cn id=225 lang=java
6+
*
7+
* [225] 用队列实现栈
8+
*/
9+
10+
// @lc code=start
11+
class MyStack {
12+
Queue<Integer> queue1;
13+
Queue<Integer> queue2;
14+
15+
/** Initialize your data structure here. */
16+
public MyStack() {
17+
queue1 = new LinkedList<>();
18+
queue2 = new LinkedList<>();
19+
}
20+
21+
/** Push element x onto stack. */
22+
public void push(int x) {
23+
queue1.add(x);
24+
}
25+
26+
/** Removes the element on top of the stack and returns that element. */
27+
public int pop() {
28+
while(queue1.size()>1){
29+
queue2.add(queue1.poll());
30+
}
31+
int temp = queue1.poll();
32+
33+
while(!queue2.isEmpty()){
34+
queue1.add(queue2.poll());
35+
}
36+
37+
return temp;
38+
}
39+
40+
/** Get the top element. */
41+
public int top() {
42+
while(queue1.size()>1){
43+
queue2.add(queue1.poll());
44+
}
45+
int temp = queue1.peek();
46+
47+
queue2.add(queue1.poll());
48+
49+
while(!queue2.isEmpty()){
50+
queue1.add(queue2.poll());
51+
}
52+
return temp;
53+
}
54+
55+
/** Returns whether the stack is empty. */
56+
public boolean empty() {
57+
return queue1.isEmpty();
58+
}
59+
}
60+
61+
/**
62+
* Your MyStack object will be instantiated and called as such:
63+
* MyStack obj = new MyStack();
64+
* obj.push(x);
65+
* int param_2 = obj.pop();
66+
* int param_3 = obj.top();
67+
* boolean param_4 = obj.empty();
68+
*/
69+
// @lc code=end
70+

232.用栈实现队列.java

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import java.util.Stack;
2+
3+
/*
4+
* @lc app=leetcode.cn id=232 lang=java
5+
*
6+
* [232] 用栈实现队列
7+
*/
8+
9+
// @lc code=start
10+
class MyQueue {
11+
12+
Stack<Integer> stack1;
13+
Stack<Integer> stack2;
14+
15+
/** Initialize your data structure here. */
16+
public MyQueue() {
17+
stack1 = new Stack<Integer>();
18+
stack2 = new Stack<Integer>();
19+
}
20+
21+
/** Push element x to the back of queue. */
22+
public void push(int x) {
23+
stack1.push(x);
24+
}
25+
26+
/** Removes the element from in front of queue and returns that element. */
27+
public int pop() {
28+
while(!stack1.empty()){
29+
stack2.push(stack1.pop());
30+
}
31+
int temp = stack2.pop();
32+
while(!stack2.empty()){
33+
stack1.push(stack2.pop());
34+
}
35+
return temp;
36+
}
37+
38+
/** Get the front element. */
39+
public int peek() {
40+
while(!stack1.empty()){
41+
stack2.push(stack1.pop());
42+
}
43+
int temp = stack2.peek();
44+
while(!stack2.empty()){
45+
stack1.push(stack2.pop());
46+
}
47+
return temp;
48+
}
49+
50+
/** Returns whether the queue is empty. */
51+
public boolean empty() {
52+
if(stack1.empty()){
53+
return true;
54+
}
55+
return false;
56+
}
57+
}
58+
59+
/**
60+
* Your MyQueue object will be instantiated and called as such:
61+
* MyQueue obj = new MyQueue();
62+
* obj.push(x);
63+
* int param_2 = obj.pop();
64+
* int param_3 = obj.peek();
65+
* boolean param_4 = obj.empty();
66+
*/
67+
// @lc code=end
68+

263.丑数.java

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=263 lang=java
3+
*
4+
* [263] 丑数
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public boolean isUgly(int num) {
10+
if(num < 1){
11+
return false;
12+
}
13+
while(num%5==0){
14+
num/=5;
15+
}
16+
while(num%3==0){
17+
num/=3;
18+
}
19+
while(num%2==0){
20+
num/=2;
21+
}
22+
if(num == 1){
23+
24+
return true;
25+
}
26+
return false;
27+
}
28+
}
29+
// @lc code=end
30+

0 commit comments

Comments
 (0)