forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
476 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Source : https://leetcode.com/problems/min-stack/description/ | ||
// Author : Tianming Cao | ||
// Date : 2018-02-02 | ||
|
||
/********************************************************************************** | ||
* Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. | ||
* | ||
* push(x) -- Push element x onto stack. | ||
* pop() -- Removes the element on top of the stack. | ||
* top() -- Get the top element. | ||
* getMin() -- Retrieve the minimum element in the stack. | ||
* Example: | ||
* MinStack minStack = new MinStack(); | ||
* minStack.push(-2); | ||
* minStack.push(0); | ||
* minStack.push(-3); | ||
* minStack.getMin(); --> Returns -3. | ||
* minStack.pop(); | ||
* minStack.top(); --> Returns 0. | ||
* minStack.getMin(); --> Returns -2. | ||
* | ||
**********************************************************************************/ | ||
package minStack; | ||
|
||
import java.util.Stack; | ||
|
||
public class MinStack { | ||
public Stack<Integer> mainStack; | ||
/** | ||
* Call an extra stack named assistStack to store the min value. | ||
* While we doing push operation, compare x with the top of assistStack and push the smaller value into assistStack. | ||
* The other operations pop,top and getMin is very simple. | ||
**/ | ||
public Stack<Integer> assistStack; | ||
public MinStack() { | ||
mainStack = new Stack<Integer>(); | ||
assistStack = new Stack<Integer>(); | ||
} | ||
public void push(int x) { | ||
mainStack.push(x); | ||
if (assistStack.isEmpty()) { | ||
assistStack.push(x); | ||
} else { | ||
assistStack.push(Math.min(x, getMin())); | ||
} | ||
} | ||
|
||
public void pop() { | ||
mainStack.pop(); | ||
assistStack.pop(); | ||
} | ||
|
||
public int top() { | ||
return mainStack.peek(); | ||
} | ||
|
||
public int getMin() { | ||
return assistStack.peek(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package minStack; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Test for 155. Min Stack | ||
*/ | ||
public class TestMinStack { | ||
@Test | ||
public void test() { | ||
MinStack minStack = new MinStack(); | ||
minStack.push(3); | ||
minStack.push(4); | ||
minStack.push(1); | ||
minStack.push(2); | ||
Assert.assertTrue(minStack.getMin() == 1); | ||
minStack.pop(); | ||
minStack.pop(); | ||
Assert.assertTrue(minStack.getMin() == 3); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Source : https://leetcode.com/problems/implement-queue-using-stacks/description/ | ||
// Author : Tianming Cao | ||
// Date : 2018-02-02 | ||
|
||
/********************************************************************************** | ||
* Implement the following operations of a queue using stacks. | ||
* | ||
* push(x) -- Push element x to the back of queue. | ||
* pop() -- Removes the element from in front of queue. | ||
* peek() -- Get the front element. | ||
* empty() -- Return whether the queue is empty. | ||
* | ||
* Notes: | ||
* You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid. | ||
* Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack. | ||
* You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue). | ||
* | ||
**********************************************************************************/ | ||
package myQueue; | ||
|
||
import java.util.Stack; | ||
|
||
/** | ||
* This problem is a sibling of problem 225(https://leetcode.com/problems/implement-stack-using-queues/description/) | ||
* The solution is: | ||
* 1. stack1 is always in charge of push operation | ||
* 2. stack2 is always in charge of peek and pop operation | ||
* 3. if we want to do peek or pop operation, but stack2 is empty, | ||
* we should first pop all the elements from stack1 and push them into stack2 in turn. | ||
* Give a Example: | ||
* | ||
* First, push numbers "1,2,3,4,5" to stack1, then stack1's structure is: | ||
* | ||
* |5| | ||
* |4| | ||
* |3| | ||
* |2| | ||
* |1| | ||
* | ||
* Second, if we want to get the front element "1",we should pop all the elements of stack1 and push them into stack2, | ||
* after this, stack1 is empty, and stack2's structrue is: | ||
* | ||
* |1| | ||
* |2| | ||
* |3| | ||
* |4| | ||
* |5| | ||
* | ||
* So we can get stack2's top element "1" as the front element of queue. | ||
* | ||
* Next, if we want to push "6" to the back of queue, we should push "6" into stack1 as before, so stack1's structure is: | ||
* | ||
* |6| | ||
* | ||
* Finally, if we want to do pop operation twice ,we should remove the top element of stack2 twice, so stack2's structure is: | ||
* | ||
* |3| | ||
* |4| | ||
* |5| | ||
* | ||
* as expect, the removed element is "1" and "2". | ||
*/ | ||
public class MyQueue { | ||
public Stack<Integer> stack1; | ||
public Stack<Integer> stack2; | ||
public int size; | ||
public MyQueue() { | ||
stack1 = new Stack<Integer>(); | ||
stack2 = new Stack<Integer>(); | ||
size = 0; | ||
} | ||
|
||
public void push(int x) { | ||
stack1.push(x); | ||
size++; | ||
} | ||
|
||
public int pop() { | ||
if (stack2.isEmpty()) { | ||
while (!stack1.isEmpty()) { | ||
stack2.push(stack1.pop()); | ||
} | ||
} | ||
int value = stack2.pop(); | ||
size--; | ||
return value; | ||
} | ||
|
||
public int peek() { | ||
if (stack2.isEmpty()) { | ||
while (!stack1.isEmpty()) { | ||
stack2.push(stack1.pop()); | ||
} | ||
} | ||
int value = stack2.peek(); | ||
return value; | ||
} | ||
|
||
public boolean empty() { | ||
return size == 0 ? true : false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package myQueue; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
/** | ||
* Test for 232. Implement Queue using Stacks | ||
*/ | ||
public class TestMyQueue { | ||
@Test | ||
public void test(){ | ||
MyQueue queue=new MyQueue(); | ||
Assert.assertTrue(queue.empty()); | ||
queue.push(1); | ||
queue.push(2); | ||
queue.push(3); | ||
queue.push(4); | ||
Assert.assertTrue(queue.pop()==1); | ||
Assert.assertTrue(queue.pop()==2); | ||
queue.push(5); | ||
Assert.assertTrue(queue.peek()==3); | ||
Assert.assertTrue(!queue.empty()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Source : https://leetcode.com/problems/implement-stack-using-queues/description/ | ||
// Author : Tianming Cao | ||
// Date : 2018-02-02 | ||
|
||
/********************************************************************************** | ||
* Implement the following operations of a stack using queues. | ||
* push(x) -- Push element x onto stack. | ||
* pop() -- Removes the element on top of the stack. | ||
* top() -- Get the top element. | ||
* empty() -- Return whether the stack is empty. | ||
* Notes: | ||
* You must use only standard operations of a queue -- which means only push to back, peek/pop from front, size, and is empty operations are valid. | ||
* Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue. | ||
* You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack). | ||
* | ||
**********************************************************************************/ | ||
package myStack; | ||
|
||
import java.util.LinkedList; | ||
import java.util.Queue; | ||
/********************************************************************************** | ||
* This problem is a sibling of problem 232(https://leetcode.com/problems/implement-queue-using-stacks/description/). | ||
* The train of thought is: | ||
* If we want to pop a element,first we should judge which queue is not empty assumed queue1,another empty queue assumed queue2. | ||
* Next,we pop all element from queue1 and push them into queue2 util queue1's size is 1. | ||
* Finally the last element in queue1 is the correct pop value. | ||
* If queue1 is empty and queue2 is not empty,the step is symmetric. | ||
* The top operation is similar with pop operation. | ||
**********************************************************************************/ | ||
public class MyStack { | ||
public Queue<Integer> queue1; | ||
public Queue<Integer> queue2; | ||
public int flag; | ||
public int size; | ||
public MyStack() { | ||
queue1=new LinkedList<Integer>(); | ||
queue2=new LinkedList<Integer>(); | ||
flag=1; | ||
size=0; | ||
} | ||
|
||
public void push(int x) { | ||
if(flag==1){ | ||
queue1.offer(x); | ||
}else{ | ||
queue2.offer(x); | ||
} | ||
size++; | ||
} | ||
|
||
public int pop() { | ||
int value; | ||
if(flag==1){ | ||
while(queue1.size()>1){ | ||
queue2.offer(queue1.poll()); | ||
} | ||
value=queue1.poll(); | ||
flag=2; | ||
}else{ | ||
while(queue2.size()>1){ | ||
queue1.offer(queue2.poll()); | ||
} | ||
value=queue2.poll(); | ||
flag=1; | ||
} | ||
size--; | ||
return value; | ||
} | ||
|
||
public int top() { | ||
if(flag==1){ | ||
while(queue1.size()>1){ | ||
queue2.offer(queue1.poll()); | ||
} | ||
int value=queue1.poll(); | ||
queue2.offer(value); | ||
flag=2; | ||
return value; | ||
}else{ | ||
while(queue2.size()>1){ | ||
queue1.offer(queue2.poll()); | ||
} | ||
int value=queue2.poll(); | ||
queue1.offer(value); | ||
flag=1; | ||
return value; | ||
} | ||
} | ||
|
||
public boolean empty() { | ||
return size==0?true:false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package myStack; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
/** | ||
* Test for 225. Implement Stack using Queues | ||
*/ | ||
public class TestMyStack { | ||
@Test | ||
public void test(){ | ||
MyStack stack=new MyStack(); | ||
stack.push(1); | ||
stack.push(2); | ||
stack.push(3); | ||
stack.push(4); | ||
Assert.assertTrue(stack.empty()==false); | ||
Assert.assertTrue(stack.pop()==4); | ||
Assert.assertTrue(stack.pop()==3); | ||
Assert.assertTrue(stack.top()==2); | ||
stack.push(5); | ||
Assert.assertTrue(stack.top()==5); | ||
} | ||
} |
Oops, something went wrong.