Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyQueue {
- Stack<Integer> in;
- Stack<Integer> out;
- public MyQueue() {
- in = new Stack<>();
- out = new Stack<>();
- }
- public void push(int x) {
- in.push(x);
- }
- public int pop() {
- if (out.isEmpty()) {
- while (!in.isEmpty()) {
- out.add(in.pop());
- }
- }
- return out.pop();
- }
- public int peek() {
- if (out.isEmpty()) {
- while (!in.isEmpty()) {
- out.add(in.pop());
- }
- }
- return out.peek();
- }
- public boolean empty() {
- return in.isEmpty() && out.isEmpty();
- }
- }
- class Solution {
- public String simplifyPath(String path) {
- Deque<String> deq = new LinkedList<>();
- String[] tokens = path.split("/");
- for (var token : tokens) {
- if ("..".equals(token)) {
- if (!deq.isEmpty()) {
- deq.pollLast();
- }
- } else if (!"".equals(token) && !".".equals(token)) {
- deq.addLast(token);
- }
- }
- if (deq.isEmpty()) {
- return "/";
- }
- StringBuilder ans = new StringBuilder();
- while (!deq.isEmpty()) {
- ans.append("/").append(deq.pollFirst());
- }
- return ans.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement