forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
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,109 @@ | ||
package com.donaldy.basic; | ||
|
||
import java.util.*; | ||
|
||
public class StackUtil { | ||
|
||
|
||
/** | ||
* 假设栈中的元素是Integer, 从栈顶到栈底是 : 5,4,3,2,1 调用该方法后, 元素次序变为: 1,2,3,4,5 | ||
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
*/ | ||
public static void reverse(Stack s) { | ||
Stack stack = new Stack(); | ||
while (!s.isEmpty()) { | ||
Object element = s.pop(); | ||
stack.push(element); | ||
} | ||
|
||
while (!stack.isEmpty()) { | ||
Object element = stack.pop(); | ||
s.push(element); | ||
} | ||
} | ||
|
||
/** | ||
* 删除栈中的某个元素 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
* | ||
* @param o | ||
*/ | ||
public static void remove(Stack s,Object o) { | ||
//若stack的值为唯一的。 | ||
Stack stack = new Stack(); | ||
while (!s.isEmpty()) { | ||
Object element = s.pop(); | ||
if (o == element) { | ||
break; | ||
} | ||
stack.push(element); | ||
} | ||
|
||
while (!stack.isEmpty()) { | ||
Object element = stack.pop(); | ||
s.push(element); | ||
} | ||
} | ||
|
||
/** | ||
* 从栈顶取得len个元素, 原来的栈中元素保持不变 | ||
* 注意:只能使用Stack的基本操作,即push,pop,peek,isEmpty, 可以使用另外一个栈来辅助 | ||
* @param len | ||
* @return | ||
*/ | ||
public static Object[] getTop(Stack s,int len) { | ||
if (len < 0 || len >= s.size()) | ||
throw new IndexOutOfBoundsException("len : " + len); | ||
Object [] arr = new Object[len]; | ||
|
||
for (int i = 0 ; i < len && !s.isEmpty(); ++i) { | ||
arr[i] = s.pop(); | ||
} | ||
return arr; | ||
} | ||
/** | ||
* 字符串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){ | ||
char [] arr = s.toCharArray(); | ||
Stack stack = new Stack(); | ||
for (int i = 0; i < s.length(); ++i) { | ||
if (arr[i] == '(' ) | ||
stack.push(')'); | ||
if ( arr[i] == '{' ) | ||
stack.push('}'); | ||
if ( arr[i] == '[') | ||
stack.push(']'); | ||
|
||
if (arr[i] == ')' ) { | ||
if (')' != (char)stack.peek()) | ||
break; | ||
stack.pop(); | ||
} | ||
|
||
if (arr[i] == '}' ) { | ||
if ('}' != (char)stack.peek()) | ||
break; | ||
stack.pop(); | ||
} | ||
|
||
if (arr[i] == ']' ) { | ||
if (']' != (char)stack.peek()) | ||
break; | ||
stack.pop(); | ||
} | ||
|
||
} | ||
|
||
if (stack.isEmpty()) | ||
return true; | ||
|
||
return false; | ||
} | ||
|
||
|
||
} |
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
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
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,65 @@ | ||
package com.donaldy.test; | ||
|
||
import com.donaldy.basic.Stack; | ||
import com.donaldy.basic.StackUtil; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Created by DonaldY on 2017/4/6. | ||
*/ | ||
public class StackUtilTest { | ||
|
||
@Test | ||
public void testReverse() { | ||
Stack stack = new Stack(); | ||
Integer [] intArr = {5, 4, 3, 2, 1}; | ||
for (int i = 4; i >= 0 ; --i) | ||
stack.push(intArr[i]); | ||
StackUtil.reverse(stack); | ||
for (int i = 0 ; i < 5; ++i) { | ||
Assert.assertEquals((int)stack.pop(), (int)intArr[i]); | ||
} | ||
} | ||
|
||
@Test | ||
public void testRemove() { | ||
Stack stack = new Stack(); | ||
Integer [] intArr = {5, 4, 3, 2, 1}; | ||
for (int i = 4; i >= 0 ; --i) | ||
stack.push(intArr[i]); | ||
|
||
StackUtil.remove(stack, 2); | ||
for (int i = 0; i < 5; ++i) { | ||
if (i == 3) | ||
continue; | ||
System.out.println("stack: " + stack.peek() + " i : " + (int) intArr[i]); | ||
Assert.assertEquals((int)stack.pop(), (int)intArr[i]); | ||
} | ||
} | ||
|
||
@Test | ||
public void testGetTop() { | ||
Stack stack = new Stack(); | ||
Integer [] intArr = {5, 4, 3, 2, 1}; | ||
for (int i = 4; i >= 0 ; --i) | ||
stack.push(intArr[i]); | ||
|
||
int len = 3; | ||
Object [] arr = StackUtil.getTop(stack, len); | ||
|
||
for (int i = 0 ; i < arr.length ; ++i) { | ||
Assert.assertEquals((int)arr[i], (int)intArr[i]); | ||
} | ||
} | ||
|
||
@Test | ||
public void testIsValidPairs() { | ||
|
||
String str1 = "([e{d}f])"; | ||
Assert.assertEquals(true, StackUtil.isValidPairs(str1)); | ||
|
||
String str2 = "([b{x]y})"; | ||
Assert.assertEquals(false, StackUtil.isValidPairs(str2)); | ||
} | ||
} |