-
Notifications
You must be signed in to change notification settings - Fork 641
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 #46 from vegetableDogBai/master
week9数据结构作业完成
- Loading branch information
Showing
3 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
group23/563253496/week9_datastructure/src/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,40 @@ | ||
//package com.coding.basic.stack; | ||
|
||
import java.util.Collections; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
|
||
/** | ||
* 设计一个栈,支持栈的push和pop操作,以及第三种操作findMin, 它返回改数据结构中的最小元素 | ||
* finMin操作最坏的情形下时间复杂度应该是O(1) , 简单来讲,操作一次就可以得到最小值 | ||
* | ||
* @author liuxin | ||
*/ | ||
public class QuickMinStack { | ||
LinkedList<Integer> list = new LinkedList<Integer>(); | ||
LinkedList<Integer> temp = new LinkedList<>(); | ||
int min = Integer.MAX_VALUE; | ||
|
||
public void push(int data) { | ||
if (data < min) { | ||
min = data; | ||
} | ||
list.add(data); | ||
} | ||
|
||
public int pop() { | ||
int num = list.remove(list.size() - 1); | ||
if (num == min) { | ||
for (int tmp : list) { | ||
temp.add(tmp); | ||
} | ||
} | ||
Collections.sort(temp); | ||
min = temp.get(0); | ||
return list.remove(list.size() - 1); | ||
} | ||
|
||
public int findMin() { | ||
return min; | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
group23/563253496/week9_datastructure/src/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,53 @@ | ||
/** | ||
* Created by bdl19 on 2017/5/6. | ||
*/ | ||
|
||
import java.util.*; | ||
|
||
|
||
public class StackWithTwoQueues { | ||
|
||
private Queue<Integer> queue1 = new LinkedList<Integer>(); | ||
private Queue<Integer> queue2 = new LinkedList<Integer>(); | ||
|
||
public void push(int data) { | ||
if(queue2.isEmpty()){ | ||
queue1.add(data); | ||
}else{ | ||
queue2.add(data); | ||
} | ||
} | ||
|
||
public int pop() { | ||
if (queue1.size() == 0 && queue2.size() == 0) { | ||
throw new IndexOutOfBoundsException(); | ||
} | ||
|
||
|
||
|
||
if(queue2.isEmpty()){ | ||
while(!(queue1.size()==1)){ | ||
queue2.add(queue1.remove()); | ||
} | ||
return queue1.remove(); | ||
|
||
}else{ | ||
while(!(queue2.size()==1)){ | ||
queue1.add(queue2.remove()); | ||
} | ||
return queue2.remove(); | ||
} | ||
|
||
|
||
} | ||
|
||
public static void main(String[] args) { | ||
StackWithTwoQueues s = new StackWithTwoQueues(); | ||
s.push(1); | ||
s.push(2); | ||
s.push(3); | ||
for (int i = 0; i < 3; i++) { | ||
System.out.println(s.pop()); | ||
} | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
group23/563253496/week9_datastructure/src/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,100 @@ | ||
/** | ||
* Created by bdl19 on 2017/5/6. | ||
*/ | ||
public class TwoStackInOneArray { | ||
Object[] data = new Object[10]; | ||
|
||
private int index1 = 0; | ||
private int index2 = data.length - 1; | ||
|
||
private boolean isFull() { | ||
if (index1 == index2) { | ||
return true; | ||
} else return false; | ||
} | ||
|
||
private void extendCapacity() { | ||
Object[] temp = new Object[data.length + data.length / 2]; | ||
for (int i = 0; i <= index1; i++) { | ||
temp[i] = data[i]; | ||
} | ||
int indext = data.length - 1; | ||
for (int i = temp.length - 1; i >= data.length - index2; i--) { | ||
temp[i] = indext; | ||
indext--; | ||
} | ||
index2 = temp.length - (temp.length - index1); | ||
this.data = temp; | ||
} | ||
|
||
/** | ||
* 向第一个栈中压入元素 | ||
* | ||
* @param o | ||
*/ | ||
public void push1(Object o) { | ||
if (this.isFull()) { | ||
extendCapacity(); | ||
} | ||
data[index1] = o; | ||
index1++; | ||
|
||
} | ||
|
||
/** | ||
* 从第一个栈中弹出元素 | ||
* | ||
* @return | ||
*/ | ||
public Object pop1() { | ||
index1--; | ||
Object o = data[index1]; | ||
data[index1] = null; | ||
return o; | ||
} | ||
|
||
/** | ||
* 获取第一个栈的栈顶元素 | ||
* | ||
* @return | ||
*/ | ||
|
||
public Object peek1() { | ||
return data[index1 - 1]; | ||
} | ||
|
||
/* | ||
* 向第二个栈压入元素 | ||
*/ | ||
public void push2(Object o) { | ||
if (this.isFull()) { | ||
extendCapacity(); | ||
} | ||
data[index2] = o; | ||
index2--; | ||
} | ||
|
||
/** | ||
* 从第二个栈弹出元素 | ||
* | ||
* @return | ||
*/ | ||
public Object pop2() { | ||
|
||
Object o = data[++index2]; | ||
data[index2] = null; | ||
return o; | ||
} | ||
|
||
/** | ||
* 获取第二个栈的栈顶元素 | ||
* | ||
* @return | ||
*/ | ||
|
||
public Object peek2() { | ||
|
||
return data[index2 + 1]; | ||
} | ||
|
||
} |