forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from zavier/master
更新第九周仓库
- Loading branch information
Showing
49 changed files
with
2,363 additions
and
14 deletions.
There are no files selected for viewing
145 changes: 145 additions & 0 deletions
145
group01/765324639/src/main/java/datastructure/stack/QuickMinStack.java
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,145 @@ | ||
package datastructure.stack; | ||
|
||
/** | ||
* 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 | ||
* finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class QuickMinStack { | ||
|
||
private int size = 0; | ||
private int index = 0; // 目前的最早入库索引 | ||
private Node head; | ||
|
||
public int findMin(){ | ||
if (head == null) { | ||
throw new RuntimeException("Stack is empty, don't have min!"); | ||
} | ||
return head.data; | ||
} | ||
|
||
public void push(int data){ | ||
if (size == 0) { | ||
head = new Node(size++, data); | ||
} else { | ||
Node temp = head; | ||
while (temp.next != null && data > temp.data) { | ||
temp = temp.next; | ||
} | ||
Node newNode = new Node(size++, data); | ||
|
||
// 未到达最后节点,或者到达最后节点,但是要插入的数据小于最后一个节点,此时插入到temp节点之前 | ||
if (unReachedLastNodeOrSmallThanLastNode(data, temp)) { | ||
insertBefore(newNode, temp); | ||
} else { // 其他情况插入到temp节点之后 | ||
insertAfter(newNode, temp); | ||
} | ||
} | ||
} | ||
|
||
private boolean unReachedLastNodeOrSmallThanLastNode(int data, Node temp) { | ||
return temp.next != null || (temp.next == null && data < temp.data); | ||
} | ||
|
||
/** | ||
* 将src节点插入到dest之前 | ||
* @param src | ||
* @param dest | ||
*/ | ||
private void insertBefore(Node src, Node dest) { | ||
if (dest.prev == null) { // dest为首个元素 | ||
head = src; | ||
} else { | ||
src.prev = dest.prev; | ||
dest.prev.next = src; | ||
} | ||
dest.prev = src; | ||
src.next = dest; | ||
} | ||
|
||
/** | ||
* 将src节点插入到dest之后 | ||
* @param src | ||
* @param dest | ||
*/ | ||
private void insertAfter(Node src, Node dest) { | ||
if (dest.next == null) { // temp为最后一个元素 | ||
dest.next = src; | ||
src.prev = dest; | ||
} else { | ||
dest = dest.next; | ||
insertBefore(src, dest); | ||
} | ||
} | ||
|
||
public int pop(){ | ||
Node temp = head; | ||
while (temp.next != null) { | ||
if (temp.index == this.index) { | ||
Node dest = remove(temp); | ||
return dest.data; | ||
} | ||
temp = temp.next; | ||
} | ||
if (temp.index == this.index) { | ||
Node dest = remove(temp); | ||
return dest.data; | ||
} | ||
throw new RuntimeException("Stack is Empty!"); | ||
} | ||
|
||
private Node remove(Node node) { | ||
if (node.prev == null && node.next == null) { // 唯一的元素,删除后将初始化size和index | ||
this.size = 0; | ||
this.index = 0; | ||
return node; | ||
} | ||
if (node.prev == null) { // 元素在开头位置 | ||
head = head.next; | ||
node.next.prev = null; | ||
node.next = null; | ||
} else if (node.next == null) { // 元素在结尾位置 | ||
node.prev.next = null; | ||
node.prev = null; | ||
} else { // 元素在中间位置 | ||
node.prev.next = node.next; | ||
node.next.prev = node.prev; | ||
node.prev = null; | ||
node.next = null; | ||
} | ||
this.size--; | ||
this.index++; | ||
return node; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
// 调试输出用 | ||
// @Override | ||
// public String toString() { | ||
// StringBuilder builder = new StringBuilder(size); | ||
// builder.append("["); | ||
// Node temp = head; | ||
// while (temp.next != null) { | ||
// builder.append(temp.data + ", "); | ||
// temp = temp.next; | ||
// } | ||
// builder.append(temp.data + "]"); | ||
// return builder.toString(); | ||
// } | ||
|
||
private static class Node { | ||
Node prev; | ||
int data; | ||
int index; // 插入的顺序 | ||
Node next; | ||
|
||
Node (int index, int data) { | ||
this.index = index; | ||
this.data = data; | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
group01/765324639/src/main/java/datastructure/stack/StackWithTwoQueues.java
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,26 @@ | ||
package datastructure.stack; | ||
|
||
import datastructure.queue.Queue; | ||
|
||
public class StackWithTwoQueues { | ||
|
||
private Queue queue1 = new Queue(); | ||
private Queue queue2 = new Queue(); | ||
|
||
public void push(int data) { | ||
queue1.enQueue(data); | ||
} | ||
|
||
public int pop() { | ||
int size = queue1.size(); | ||
for (int i = 0; i < size - 1; i++) { | ||
queue2.enQueue(queue1.deQueue()); | ||
} | ||
int dest = (int) queue1.deQueue(); | ||
queue1 = queue2; | ||
queue2 = queue1; | ||
return dest; | ||
} | ||
|
||
|
||
} |
83 changes: 83 additions & 0 deletions
83
group01/765324639/src/main/java/datastructure/stack/TwoStackInOneArray.java
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,83 @@ | ||
package datastructure.stack; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* 用一个数组实现两个栈 | ||
* 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class TwoStackInOneArray { | ||
Object[] data = new Object[10]; | ||
|
||
private int index1 = 0; | ||
private int index2 = data.length - 1; | ||
|
||
/** | ||
* 向第一个栈中压入元素 | ||
* @param o | ||
*/ | ||
public void push1(Object o){ | ||
ensureCapacity(); | ||
data[index1++] = o; | ||
} | ||
|
||
private void ensureCapacity() { | ||
if (isFull()) { | ||
grow(); | ||
} | ||
} | ||
|
||
private boolean isFull() { | ||
return index1 + 1 == index2; | ||
} | ||
|
||
private void grow() { | ||
int stack2Length = data.length - 1 - index2; | ||
data = Arrays.copyOf(data, data.length * 2); | ||
int oldIndex2 = index2; | ||
index2 = data.length - stack2Length - 1; | ||
System.arraycopy(data, oldIndex2 + 1, data, index2 + 1, stack2Length); | ||
} | ||
|
||
/** | ||
* 从第一个栈中弹出元素 | ||
* @return | ||
*/ | ||
public Object pop1(){ | ||
return data[--index1]; | ||
} | ||
|
||
/** | ||
* 获取第一个栈的栈顶元素 | ||
* @return | ||
*/ | ||
|
||
public Object peek1(){ | ||
return data[index1 - 1]; | ||
} | ||
/** | ||
* 向第二个栈压入元素 | ||
*/ | ||
public void push2(Object o){ | ||
ensureCapacity(); | ||
data[index2--] = o; | ||
} | ||
/** | ||
* 从第二个栈弹出元素 | ||
* @return | ||
*/ | ||
public Object pop2(){ | ||
return data[++index2]; | ||
} | ||
/** | ||
* 获取第二个栈的栈顶元素 | ||
* @return | ||
*/ | ||
|
||
public Object peek2(){ | ||
return data[index2 + 1]; | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
group01/765324639/src/test/java/datastructure/stack/QuickMinStackTest.java
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,42 @@ | ||
package datastructure.stack; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class QuickMinStackTest { | ||
|
||
@Test | ||
public void test() { | ||
QuickMinStack stack = new QuickMinStack(); | ||
stack.push(5); | ||
stack.push(-5); | ||
stack.push(50); | ||
stack.push(23); | ||
stack.push(0); | ||
stack.push(100); | ||
|
||
assertEquals(-5, stack.findMin()); | ||
assertEquals(6, stack.size()); | ||
|
||
assertEquals(5, stack.pop()); | ||
assertEquals(-5, stack.findMin()); | ||
|
||
assertEquals(-5, stack.pop()); | ||
assertEquals(0, stack.findMin()); | ||
|
||
assertEquals(50, stack.pop()); | ||
assertEquals(0, stack.findMin()); | ||
|
||
assertEquals(23, stack.pop()); | ||
assertEquals(0, stack.findMin()); | ||
|
||
assertEquals(0, stack.pop()); | ||
assertEquals(100, stack.findMin()); | ||
|
||
assertEquals(100, stack.pop()); | ||
|
||
assertEquals(0, stack.size()); | ||
|
||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
group01/765324639/src/test/java/datastructure/stack/StackWithTwoQueuesTest.java
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,26 @@ | ||
package datastructure.stack; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class StackWithTwoQueuesTest { | ||
|
||
@Test | ||
public void test() { | ||
StackWithTwoQueues stack = new StackWithTwoQueues(); | ||
for (int i = 0; i < 500; i++) { | ||
stack.push(i); | ||
} | ||
for (int i = 0; i < 500; i++) { | ||
assertEquals(500 - i - 1, stack.pop()); | ||
} | ||
|
||
stack.push(50); | ||
stack.push(60); | ||
assertEquals(60, stack.pop()); | ||
stack.push(70); | ||
assertEquals(70, stack.pop()); | ||
assertEquals(50, stack.pop()); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
group01/765324639/src/test/java/datastructure/stack/TwoStackInOneArrayTest.java
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,42 @@ | ||
package datastructure.stack; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Test; | ||
|
||
public class TwoStackInOneArrayTest { | ||
|
||
@Test | ||
public void test() { | ||
TwoStackInOneArray stack = new TwoStackInOneArray(); | ||
for (int i = 0; i < 500; i++) { | ||
stack.push1("stack1:" + i); | ||
} | ||
for (int i = 0; i < 500; i++) { | ||
stack.push2("stack2:" + i); | ||
} | ||
|
||
for (int i = 0; i < 500; i++) { | ||
assertEquals("stack1:" + (500 - i - 1), stack.peek1()); | ||
assertEquals("stack1:" + (500 - i - 1), stack.pop1()); | ||
} | ||
for (int i = 0; i < 500; i++) { | ||
assertEquals("stack2:" + (500 - i - 1), stack.peek2()); | ||
assertEquals("stack2:" + (500 - i - 1), stack.pop2()); | ||
} | ||
|
||
stack.push1(50); | ||
stack.push1(60); | ||
assertEquals(60, stack.pop1()); | ||
stack.push1(70); | ||
assertEquals(70, stack.pop1()); | ||
assertEquals(50, stack.pop1()); | ||
|
||
stack.push2(50); | ||
stack.push2(60); | ||
assertEquals(60, stack.pop2()); | ||
stack.push2(70); | ||
assertEquals(70, stack.pop2()); | ||
assertEquals(50, stack.pop2()); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Oops, something went wrong.