Skip to content

Commit 962ddbb

Browse files
committed
Java solution 232 && 316
1 parent 67cbbd1 commit 962ddbb

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Implement the following operations of a queue using stacks.
5+
* <p>
6+
* push(x) -- Push element x to the back of queue.
7+
* pop() -- Removes the element from in front of queue.
8+
* peek() -- Get the front element.
9+
* empty() -- Return whether the queue is empty.
10+
* <p>
11+
* Notes:
12+
* You must use only standard operations of a stack -- which means only push to top, peek/pop from top, size, and is empty operations are valid.
13+
* Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
14+
* You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
15+
* <p>
16+
* Created by drfish on 6/8/2017.
17+
*/
18+
public class _232ImplementQueueUsingStacks {
19+
public class MyQueue {
20+
private Stack<Integer> stack1;
21+
private Stack<Integer> stack2;
22+
23+
/**
24+
* Initialize your data structure here.
25+
*/
26+
public MyQueue() {
27+
stack1 = new Stack<>();
28+
stack2 = new Stack<>();
29+
}
30+
31+
/**
32+
* Push element x to the back of queue.
33+
*/
34+
public void push(int x) {
35+
stack1.push(x);
36+
}
37+
38+
/**
39+
* Removes the element from in front of queue and returns that element.
40+
*/
41+
public int pop() {
42+
if (stack2.isEmpty()) {
43+
while (!stack1.isEmpty()) {
44+
stack2.push(stack1.pop());
45+
}
46+
}
47+
return stack2.pop();
48+
49+
}
50+
51+
/**
52+
* Get the front element.
53+
*/
54+
public int peek() {
55+
if (stack2.isEmpty()) {
56+
while (!stack1.isEmpty()) {
57+
stack2.push(stack1.pop());
58+
}
59+
}
60+
return stack2.peek();
61+
}
62+
63+
/**
64+
* Returns whether the queue is empty.
65+
*/
66+
public boolean empty() {
67+
return stack1.isEmpty() && stack2.isEmpty();
68+
}
69+
}
70+
}

java/_316RemoveDuplicateLetters.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.Stack;
2+
3+
/**
4+
* Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order among all possible results.
5+
* <p>
6+
* Example:
7+
* Given "bcabc"
8+
* Return "abc"
9+
* <p>
10+
* Given "cbacdcbc"
11+
* Return "acdb"
12+
* <p>
13+
* Credits:
14+
* Special thanks to @dietpepsi for adding this problem and creating all test cases.
15+
* <p>
16+
* Created by drfish on 6/8/2017.
17+
*/
18+
public class _316RemoveDuplicateLetters {
19+
public String removeDuplicateLetters(String s) {
20+
if (s == null) {
21+
return null;
22+
}
23+
int[] count = new int[26];
24+
boolean[] visited = new boolean[26];
25+
char[] chars = s.toCharArray();
26+
for (char c : chars) {
27+
count[c - 'a']++;
28+
}
29+
Stack<Character> stack = new Stack<>();
30+
int index;
31+
for (char c : chars) {
32+
index = c - 'a';
33+
count[index]--;
34+
if (visited[index]) {
35+
continue;
36+
}
37+
while (!stack.isEmpty() && c < stack.peek() && count[stack.peek() - 'a'] != 0) {
38+
visited[stack.pop() - 'a'] = false;
39+
}
40+
stack.push(c);
41+
visited[index] = true;
42+
}
43+
StringBuilder sb = new StringBuilder();
44+
while (!stack.isEmpty()) {
45+
sb.insert(0, stack.pop());
46+
}
47+
return sb.toString();
48+
}
49+
50+
public static void main(String[] args) {
51+
_316RemoveDuplicateLetters solution = new _316RemoveDuplicateLetters();
52+
assert "abc".equals(solution.removeDuplicateLetters("bcabc"));
53+
assert "acdb".equals(solution.removeDuplicateLetters("cbacdcbc"));
54+
}
55+
}

0 commit comments

Comments
 (0)