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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.johnChnia.coding2017.basic;
package com.johnChnia.coding2017.basic.stack;

import com.johnChnia.coding2017.basic.linklist.LinkedList;

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

/**
* Created by john on 2017/4/7.
*/
public class StackUtil {


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

/**
* @param item 插入底部的元素
* @param s 栈对象引用
*/
private static <E> void insertAtBottom(E item, Stack<E> s) {
if (s.empty()) {
s.push(item);
} else {
E temp = s.pop();
insertAtBottom(item, s);
s.push(temp);
}
}


/**
* 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*
* @param o
*/
public static <E> void remove(Stack<E> s, Object o) {
if (s.empty()) {
return;
}
E item = s.pop();
if (!o.equals(item)) { //没有考虑o为null的情况
remove(s, o);
s.push(item);
}
}

/**
* 从栈顶取得len个元素, 原来的栈中元素保持不变
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
*
* @param len
* @return
*/
public static <E> Object[] getTop(Stack<E> s, int len) {
if (len > s.size()) {
throw new IllegalArgumentException("Len: " + len + ", Size" + s.size());
}
Object[] array = new Object[len];
int index = 0;
getArray(s, array, index);
return array;
}

/**
* 采用递归的方式把len个元素加到数组中,且保持原栈中元素不变。
*
* @param s 栈
* @param array Object数组
* @param index 数组索引
*/
private static <E> void getArray(Stack<E> s, Object[] array, int index) {
if (s.empty() || index == array.length) {
return;
}
E item = s.pop();
array[index++] = item;
getArray(s, array, index);
s.push(item);
}

/**
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz
* 使用堆栈检查字符串s中的括号是不是成对出现的。
* 例如s = "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true
* 如果 s = "([b{x]y})", 则该字符串中的括号不是成对出现的, 该方法返回false;
*
* @param s
* @return
*/
public static boolean isValidPairs(String s) { // last unclosed first closed
Stack<String> stack = new Stack<>();
for (int i = 0; i < s.length(); i++) {
String subStr = s.substring(i, i + 1);
if ("([{".contains(subStr)) {
stack.push(subStr);
} else if (")]}".contains(subStr)) {
if (stack.empty()) {
return false;
}
String left = stack.pop();
if (subStr.equals(")")) {
if(!left.equals("("))
return false;
}else if(subStr.equals("]")){
if(!left.equals("["))
return false;
}else if(subStr.equals("}")){
if(!left.equals("{"))
return false;
}
}
}
return stack.empty();
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.johnChnia.coding2017.basic;

import com.johnChnia.coding2017.basic.stack.Stack;
import org.junit.Before;
import org.junit.Test;

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

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import static com.johnChnia.coding2017.basic.stack.StackUtil.*;


/**
* Created by john on 2017/4/7.
*/
public class StackUtilTest {

Stack<Integer> s1;
Stack<Integer> s2;
Stack<Integer> s3;

@Before
public void setUp() throws Exception {
s1 = new Stack<>();
s2 = new Stack<>();
s3 = new Stack<>();

}

@Test
public void testReverse() throws Exception {
for (int i = 0; i < 4; i++) {
s1.push(i);
}
reverse(s1);
Assert.assertEquals("0→1→2→3", s1.toString());
}

@Test
public void testRemove() throws Exception {
for (int i = 0; i < 4; i++) {
s2.push(i);
}
remove(s2, 1);
Assert.assertEquals("3→2→0", s2.toString());
}

@Test
public void testGetTop() throws Exception {
for (int i = 0; i < 4; i++) {
s3.push(i);
}
Object[] array = getTop(s3, 2);
Assert.assertEquals(array.length, 2);
Assert.assertEquals(array[0], 3);
Assert.assertEquals(array[1], 2);
Assert.assertEquals("3→2→1→0", s3.toString());

}

@Test
public void testIsValidPairs() throws Exception {
String s1 = "([e{d}f])";
Assert.assertTrue(isValidPairs(s1));
String s2 = "([b{x]y})";
Assert.assertFalse(isValidPairs(s2));
}

}