1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| package com.demo.s71;
import java.util.Deque; import java.util.LinkedList;
public class Solution { public String simplifyPath(String path) { Deque<String> queue = new LinkedList<>(); String[] res = path.split("/"); for(int i = 0; i < res.length; i++){ String s = res[i]; if(s.equals(".") || s.equals("")) continue; else if (s.equals("..")){ if(!queue.isEmpty()){ queue.pollLast(); } }else{ queue.offer(s); } } StringBuilder sb = new StringBuilder("/"); while(!queue.isEmpty()){ sb.append(queue.poll()); if(!queue.isEmpty()){ sb.append("/"); } } return sb.toString().equals("") ? "/" : sb.toString(); } }
|