forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #57 from GordenChow/master
Jvm第六次作业提交
- Loading branch information
Showing
3 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
group27/513274874/data-structure/src/com/coding/basic/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,42 @@ | ||
package com.coding.basic.stack; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
|
||
/** | ||
* 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 | ||
* finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class QuickMinStack { | ||
|
||
//存储栈中元素,升序排列,minAscList.get(0)即能取出 | ||
private List<Integer> minAscList = new ArrayList<>(); | ||
|
||
private Stack stack; | ||
|
||
public void push(int data){ | ||
stack.push(data); | ||
minAscList.add(data); | ||
minAscList.sort(new Comparator<Integer>() { | ||
@Override | ||
public int compare(Integer o1, Integer o2) { | ||
return o1.compareTo(o2); | ||
} | ||
}); | ||
} | ||
public int pop(){ | ||
|
||
int val = (Integer)stack.pop(); | ||
minAscList.remove(new Integer(val)); | ||
|
||
return val; | ||
} | ||
public int findMin(){ | ||
return minAscList.get(0); | ||
} | ||
|
||
|
||
} |
45 changes: 45 additions & 0 deletions
45
group27/513274874/data-structure/src/com/coding/basic/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,45 @@ | ||
package com.coding.basic.stack; | ||
|
||
|
||
import com.coding.basic.queue.Queue; | ||
|
||
public class StackWithTwoQueues { | ||
|
||
Queue<Integer> q1 = new Queue<>(); | ||
Queue<Integer> q2 = new Queue<>(); | ||
|
||
|
||
public void push(int data) { | ||
if (q1.isEmpty()) { | ||
q2.enQueue(data); | ||
} else { | ||
q1.enQueue(data); | ||
} | ||
} | ||
|
||
public int pop() { | ||
if (!q1.isEmpty()) { | ||
while (!q1.isEmpty()) { | ||
int val = q1.deQueue(); | ||
if (q1.isEmpty()) { | ||
q2.enQueue(val); | ||
} else { | ||
return val; | ||
} | ||
} | ||
} else { | ||
while (!q2.isEmpty()) { | ||
int val = q2.deQueue(); | ||
if (!q2.isEmpty()) { | ||
q1.enQueue(val); | ||
} else { | ||
return val; | ||
} | ||
} | ||
|
||
} | ||
return 0; | ||
} | ||
|
||
|
||
} |
57 changes: 57 additions & 0 deletions
57
group27/513274874/data-structure/src/com/coding/basic/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,57 @@ | ||
package com.coding.basic.stack; | ||
|
||
/** | ||
* 用一个数组实现两个栈 | ||
* 将数组的起始位置看作是第一个栈的栈底,将数组的尾部看作第二个栈的栈底,压栈时,栈顶指针分别向中间移动,直到两栈顶指针相遇,则扩容。 | ||
* @author liuxin | ||
* | ||
*/ | ||
public class TwoStackInOneArray { | ||
Object[] data = new Object[10]; | ||
|
||
/** | ||
* 向第一个栈中压入元素 | ||
* @param o | ||
*/ | ||
public void push1(Object o){ | ||
|
||
} | ||
/** | ||
* 从第一个栈中弹出元素 | ||
* @return | ||
*/ | ||
public Object pop1(){ | ||
return null; | ||
} | ||
|
||
/** | ||
* 获取第一个栈的栈顶元素 | ||
* @return | ||
*/ | ||
|
||
public Object peek1(){ | ||
return null; | ||
} | ||
/* | ||
* 向第二个栈压入元素 | ||
*/ | ||
public void push2(Object o){ | ||
|
||
} | ||
/** | ||
* 从第二个栈弹出元素 | ||
* @return | ||
*/ | ||
public Object pop2(){ | ||
return null; | ||
} | ||
/** | ||
* 获取第二个栈的栈顶元素 | ||
* @return | ||
*/ | ||
|
||
public Object peek2(){ | ||
return null; | ||
} | ||
|
||
} |