题解 | #牛群的危险路径#
牛群的危险路径
https://www.nowcoder.com/practice/c3326fb3ac0c4a658305483482943074
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param path string字符串 * @return string字符串 */ public String find_dangerous_cow_path(String path) { // write code here Deque<String> deque = new LinkedList<>(); String[] strs = path.split("/"); for (int i = 0; i < strs.length; i++) { if(strs[i].isEmpty()||strs[i].equals(".")){ continue; }else if(strs[i].equals("..")){ if(!deque.isEmpty()){ deque.pop(); } }else { deque.push(strs[i]); } } StringBuilder stringBuilder = new StringBuilder(); while(!deque.isEmpty()){ System.out.println(deque.peekFirst()); stringBuilder.insert(0,"/"+deque.removeFirst()); } return stringBuilder.equals("")?"/":stringBuilder.toString(); } }