Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 50eff56

Browse filesBrowse files
committed
Add stack code
1 parent f3e1ef1 commit 50eff56
Copy full SHA for 50eff56

2 files changed

+72-7Lines changed: 72 additions & 7 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/test/java/datastructure/stack/IsPalindromeTest.java‎

Copy file name to clipboardExpand all lines: src/test/java/datastructure/stack/IsPalindromeTest.java
+11-7Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,22 @@ public class IsPalindromeTest {
1717
@Test
1818
public void test() {
1919
assertThat(isPalindrome("abba"), is(true));
20+
assertThat(isPalindrome("dabccbad"), is(true));
21+
assertThat(isPalindrome("abcba"), is(true));
22+
assertThat(isPalindrome("fabccdedccbaf"), is(true));
23+
assertThat(isPalindrome("fabccdedccbf"), is(false));
2024
}
2125

2226
public boolean isPalindrome(String str) {
2327
Stack<Character> stack = new Stack<>();
2428
char[] charArr = str.toCharArray();
25-
for (int i = 0; i < str.length(); i++) {
26-
if (i <= str.length() / 2) {
27-
stack.add(charArr[i]);
28-
} else {
29-
if (stack.pop() != charArr[i]) {
30-
return false;
31-
}
29+
for (int i = 0; i < str.length() / 2; i++) {
30+
stack.add(charArr[i]);
31+
}
32+
33+
for (int i = (str.length() + 1)/2; i < str.length(); i++) {
34+
if (stack.pop() != charArr[i]) {
35+
return false;
3236
}
3337
}
3438
return true;
Collapse file
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package datastructure.stack;
2+
3+
import org.junit.Test;
4+
5+
import java.util.Stack;
6+
7+
import static org.hamcrest.CoreMatchers.is;
8+
import static org.junit.Assert.assertThat;
9+
10+
11+
public class MinimumStackTest {
12+
13+
/*
14+
TASK
15+
Stack에 저장된 값들 중 최소값을 반환하는 minStack() 함수를 구현한다.
16+
*/
17+
18+
@Test
19+
public void test() {
20+
MyStack stack = new MyStack();
21+
stack.push(3);
22+
stack.push(5);
23+
stack.push(4);
24+
stack.push(2);
25+
stack.push(6);
26+
27+
assertThat(stack.min(), is(2));
28+
}
29+
30+
public class MyStack {
31+
private Stack<Integer> stack;
32+
private Stack<Integer> minStack;
33+
34+
public MyStack() {
35+
stack = new Stack<>();
36+
minStack = new Stack<>();
37+
}
38+
39+
public void push(int newVal) {
40+
if (minStack.isEmpty() || newVal <= minStack.peek()) {
41+
minStack.push(newVal);
42+
}
43+
stack.push(newVal);
44+
}
45+
46+
public int pop() {
47+
int val = stack.pop();
48+
if (!minStack.isEmpty() && val == minStack.peek()) {
49+
minStack.pop();
50+
}
51+
return val;
52+
}
53+
54+
public int min() {
55+
if (minStack.isEmpty()) {
56+
throw new RuntimeException("");
57+
}
58+
return minStack.peek();
59+
}
60+
}
61+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.