Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 98 additions & 7 deletions liuxin/data-structure/src/com/coding/basic/stack/StackUtil.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,65 @@
package com.coding.basic.stack;

import java.util.Stack;
public class StackUtil {

public static void bad_reverse(Stack<Integer> s) {
if(s == null || s.isEmpty()){
return;
}
Stack<Integer> tmpStack = new Stack();
while(!s.isEmpty()){
tmpStack.push(s.pop());
}

s = tmpStack;

}

/**
* 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*/
public static void reverse(Stack s) {

public static void reverse(Stack<Integer> s) {
if(s == null || s.isEmpty()){
return;
}
Integer top = s.pop();
reverse(s);
addToBottom(s,top);


}
public static void addToBottom(Stack<Integer> s, Integer value){
if(s.isEmpty()){
s.push(value);
} else{
Integer top = s.pop();
addToBottom(s,value);
s.push(top);
}

}

/**
* 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*
* @param o
*/
public static void remove(Stack s,Object o) {

if(s == null || s.isEmpty()){
return;
}
Stack tmpStack = new Stack();

while(!s.isEmpty()){
Object value = s.pop();
if(!value.equals(o)){
tmpStack.push(value);
}
}

while(!tmpStack.isEmpty()){
s.push(tmpStack.pop());
}
}

/**
Expand All @@ -27,7 +69,24 @@ public static void remove(Stack s,Object o) {
* @return
*/
public static Object[] getTop(Stack s,int len) {
return null;

if(s == null || s.isEmpty() || s.size()<len || len <=0 ){
return null;
}

Stack tmpStack = new Stack();
int i = 0;
Object[] result = new Object[len];
while(!s.isEmpty()){
Object value = s.pop();
tmpStack.push(value);
result[i++] = value;
if(i == len){
break;
}
}

return result;
}
/**
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz
Expand All @@ -38,7 +97,39 @@ public static Object[] getTop(Stack s,int len) {
* @return
*/
public static boolean isValidPairs(String s){
return false;

Stack<Character> stack = new Stack();
for(int i=0;i<s.length();i++){
char c = s.charAt(i);

if(c == '(' || c =='[' || c == '{'){

stack.push(c);

} else if( c == ')'){

char topChar = stack.pop();
if(topChar != '('){
return false;
}

} else if( c == ']'){

char topChar = stack.pop();
if(topChar != '['){
return false;
}

} else if( c == '}'){

char topChar = stack.pop();
if(topChar != '{'){
return false;
}

}
}
return stack.size() == 0;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.coding.basic.stack;

import static org.junit.Assert.fail;

import java.util.Stack;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
public class StackUtilTest {

@Before
public void setUp() throws Exception {
}

@After
public void tearDown() throws Exception {
}

@Test
public void testAddToBottom() {
Stack<Integer> s = new Stack();
s.push(1);
s.push(2);
s.push(3);

StackUtil.addToBottom(s, 0);

Assert.assertEquals("[0, 1, 2, 3]", s.toString());

}
@Test
public void testReverse() {
Stack<Integer> s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString());
StackUtil.reverse(s);
Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString());
}

@Test
public void testRemove() {
Stack<Integer> s = new Stack();
s.push(1);
s.push(2);
s.push(3);
StackUtil.remove(s, 2);
Assert.assertEquals("[1, 3]", s.toString());
}

@Test
public void testGetTop() {
Stack<Integer> s = new Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
{
Object[] values = StackUtil.getTop(s, 3);
Assert.assertEquals(5, values[0]);
Assert.assertEquals(4, values[1]);
Assert.assertEquals(3, values[2]);
}
}

@Test
public void testIsValidPairs() {
Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])"));
Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})"));
}

}