Java AC
import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Scanner; import java.util.regex.Pattern; public class Main { /* * 1090 */ public static void main(String[] args) throws Exception { Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { int n = scanner.nextInt(); List<String> arrList = new ArrayList<String>(); for (int i = 0; i < n; i++) { String tempStr = scanner.next(); String tempArr[] = tempStr.split(Pattern.quote("\\")); String fir = tempArr[0]; if (!arrList.contains(fir)) { arrList.add(fir); } for (int j = 1; j < tempArr.length; j++) { fir += "\\" + tempArr[j]; if (!arrList.contains(fir)) { arrList.add(fir); } } } Collections.sort(arrList); int size = arrList.size(); StringBuffer sb = new StringBuffer(); List<String> couList = new ArrayList<String>(); for (int i = 0; i < size; i++) { String tempStr = arrList.get(i); String tempArr[] = tempStr.split(Pattern.quote("\\")); int count = 0; for (int j = 0; j < tempArr.length; j++) { String str = ""; if (j != 0) { count += tempArr[j-1].length(); count += 1; int k = 0; while (k < count) { str += " "; k++; } } str += tempArr[j]; if (!couList.contains(str)) { couList.add(str); sb.append(str + "\n"); } } } System.out.println(sb); } } } /************************************************************** Problem: 1090 User: wangzhenqing Language: Java Result: Accepted Time:100 ms Memory:15804 kb ****************************************************************/2、LeetCode Simplify Path Total Accepted: 5778 Total Submissions: 29983 My Submissions
Java AC
public class Solution { public String simplifyPath(String path) { Stack<String> stack = new Stack<String>(); int len = path.length(); char array[] = path.toCharArray(); for(int i = 0; i < len; i++){ if(array[i] == '/'){ StringBuilder sb = new StringBuilder(); int j = i+1; while(j < len && array[j] != '/'){ sb.append(String.valueOf(array[j])); j++; } i = j - 1; String tempStr = sb.toString(); if(tempStr.length() == 0){ continue; } if(tempStr.equals("..")){ if(!stack.isEmpty()){ stack.pop(); } }else if(!tempStr.equals(".")){ stack.push(tempStr); } } } if(stack.isEmpty()){ return "/"; } String finalPath = ""; while(!stack.isEmpty()){ finalPath = "/" + stack.peek() + finalPath; stack.pop(); } return finalPath; } }