Skip to content

Commit 25b64ac

Browse files
author
SJsunshine
committed
StackUtil实现
1 parent 9c124a9 commit 25b64ac

File tree

2 files changed

+197
-0
lines changed

2 files changed

+197
-0
lines changed
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package week5.jvm;
2+
3+
import java.util.Stack;
4+
5+
public class StackUtil {
6+
7+
/**
8+
* 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5
9+
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
10+
*/
11+
public static void reverse(Stack<Integer> s) {
12+
13+
nullCheck(s);
14+
15+
int size = s.size();
16+
Stack<Integer> tempStack = new Stack<Integer>();
17+
18+
for(int i=0;i<size;i++){
19+
Integer top=s.pop();
20+
while(s.size() > i){
21+
tempStack.push(s.pop());
22+
}
23+
s.push(top);
24+
while(tempStack.size() > 0){
25+
s.push(tempStack.pop());
26+
}
27+
}
28+
}
29+
30+
/**
31+
* 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助
32+
*
33+
* @param o
34+
*/
35+
public static void remove(Stack s, Object o) {
36+
nullCheck(s);
37+
38+
Stack tempStack=new Stack();
39+
40+
while(!s.isEmpty()){
41+
Object top=s.pop();
42+
if(!o.equals(top)){
43+
tempStack.push(top);
44+
}
45+
}
46+
47+
while(!tempStack.isEmpty()){
48+
s.push(tempStack.pop());
49+
}
50+
}
51+
52+
/**
53+
* 从栈顶取得len个元素, 原来的栈中元素保持不变 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty,
54+
* 可以使用另外一个栈来辅助
55+
*
56+
* @param len
57+
* @return
58+
*/
59+
public static Object[] getTop(Stack s, int len) {
60+
nullCheck(s);
61+
62+
if(s.size() < len || len <=0){
63+
return null;
64+
}
65+
66+
Object[] objects=new Object[len];
67+
68+
for(int i=0;i<len;i++){
69+
objects[i]=s.pop();
70+
}
71+
72+
for(int i=len-1;i >= 0;i--){
73+
s.push(objects[i]);
74+
}
75+
76+
return objects;
77+
}
78+
79+
/**
80+
* 字符串s 可能包含这些字符: ( ) [ ] { }, a,b,c... x,yz 使用堆栈检查字符串s中的括号是不是成对出现的。 例如s =
81+
* "([e{d}f])" , 则该字符串中的括号是成对出现, 该方法返回true 如果 s = "([b{x]y})",
82+
* 则该字符串中的括号不是成对出现的, 该方法返回false;
83+
*
84+
* @param s
85+
* @return
86+
*/
87+
public static boolean isValidPairs(String s) {
88+
Stack<Character> stack=new Stack<Character>();
89+
90+
for(int i=0;i<s.length();i++){
91+
92+
boolean flag=true;
93+
Character ch=s.charAt(i);
94+
95+
switch(ch){
96+
case '(':
97+
case '[':
98+
case '{':
99+
stack.push(ch);
100+
break;
101+
case ')':
102+
if( !(!stack.isEmpty() && stack.pop() == '(') ){
103+
flag=false;
104+
}
105+
break;
106+
case ']':
107+
if( !(!stack.isEmpty() && stack.pop() == '[') ){
108+
flag=false;
109+
}
110+
break;
111+
case '}':
112+
if( !(!stack.isEmpty() && stack.pop() == '{') ){
113+
flag=false;
114+
}
115+
break;
116+
}
117+
118+
if(!flag){
119+
return false;
120+
}
121+
}
122+
return true;
123+
}
124+
125+
private static void nullCheck(Stack<Integer> s) {
126+
if (s == null || s.isEmpty()) {
127+
return;
128+
}
129+
}
130+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package week5.jvm.test;
2+
3+
import java.util.Stack;
4+
5+
import org.junit.After;
6+
import org.junit.Assert;
7+
import org.junit.Before;
8+
import org.junit.Test;
9+
10+
import week5.jvm.StackUtil;
11+
public class StackUtilTest {
12+
13+
@Before
14+
public void setUp() throws Exception {
15+
}
16+
17+
@After
18+
public void tearDown() throws Exception {
19+
}
20+
21+
22+
@Test
23+
public void testReverse() {
24+
Stack<Integer> s = new Stack();
25+
s.push(1);
26+
s.push(2);
27+
s.push(3);
28+
s.push(4);
29+
s.push(5);
30+
Assert.assertEquals("[1, 2, 3, 4, 5]", s.toString());
31+
StackUtil.reverse(s);
32+
Assert.assertEquals("[5, 4, 3, 2, 1]", s.toString());
33+
}
34+
35+
@Test
36+
public void testRemove() {
37+
Stack<Integer> s = new Stack();
38+
s.push(1);
39+
s.push(3);
40+
s.push(2);
41+
StackUtil.remove(s, 2);
42+
Assert.assertEquals("[1, 3]", s.toString());
43+
}
44+
45+
@Test
46+
public void testGetTop() {
47+
Stack<Integer> s = new Stack();
48+
s.push(1);
49+
s.push(2);
50+
s.push(3);
51+
s.push(4);
52+
s.push(5);
53+
{
54+
Object[] values = StackUtil.getTop(s, 3);
55+
Assert.assertEquals(5, values[0]);
56+
Assert.assertEquals(4, values[1]);
57+
Assert.assertEquals(3, values[2]);
58+
}
59+
}
60+
61+
@Test
62+
public void testIsValidPairs() {
63+
Assert.assertTrue(StackUtil.isValidPairs("([e{d}f])"));
64+
Assert.assertFalse(StackUtil.isValidPairs("([b{x]y})"));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)