代码:
- class Solution {
- public String simplifyPath(String path) {
- String[] names = path.split("/");
- Deque
stack = new ArrayDeque(); - for(String name: names){
- if(name.equals("..")){
- if(!stack.isEmpty()){
- stack.pollLast();
- }
- }else if(name.length()>0&&!".".equals(name)){
- stack.offerLast(name);
- }
- }
- StringBuffer ans = new StringBuffer();
- if(stack.isEmpty()){
- ans.append('/');
- }else{
- while(!stack.isEmpty()){
- ans.append('/');
- ans.append(stack.pollFirst());
- }
- }
- return ans.toString();
- }
- }
stack的一些用法