1. 程式人生 > >【九度】題目1090:路徑列印 && 【LeetCode】Simplify Path

【九度】題目1090:路徑列印 && 【LeetCode】Simplify Path


時間限制:1 秒記憶體限制:32 兆特殊判題:否提交:1319解決:230
題目描述:
給你一串路徑,譬如:
a\b\c
a\d\e
b\cst
d\
你把這些路徑中蘊含的目錄結構給畫出來,子目錄直接列在父目錄下面,並比父目錄向右縮一格,就像這樣:
a
  b
    c
  d  
    e
b
  cst
d
同一級的需要按字母順序排列,不能亂。
輸入:
    每個測試案例第一行為一個正整數n(n<=10)表示有n個路徑,當n為0時,測試結束,接下來有n行,每行有一個字串表示一個路徑,長度小於50。
輸出:
輸出目錄結構,每一個測試樣例的輸出緊跟一個空行。
樣例輸入:
4
a\b\c
a\d\e
b\cst
d\
0
樣例輸出:
a
  b
    c
  d
    e
b
  cst
d
來源:
2005年上海交通大學計算機研究生機試真題
答疑:
解題遇到問題?分享解題心得?討論本題請訪問:http://t.jobdu.com/thread-7813-1-1.html
【解題思路】
理論是可以建樹去做的,但是好像有點複雜,簡單直接的資料結構可以解決。
整體的解決思路可以分為兩部分:
1)、整理所有的路徑。
2)、按照規則列印路徑。這裡注意子目錄的縮排長度是父目錄本身長度+父目錄個數+1
測試用例,打印出的結果應該是:
4
a\ultra\c
a\d\e
b\wang\cst
d\cmd
a
  d
    e
  ultra
        c
b
  wang
       cst
d
  cmd

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
****************************************************************/
Total Accepted: 5778 Total Submissions: 29983 My Submissions
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = "/home/", => "/home"
path = "/a/./b/../../c/", => "/c"
click to show corner cases.
Corner Cases:
Did you consider the case where path = "/../"?
In this case, you should return "/".
Another corner case is the path might contain multiple slashes '/' together, such as "/home//foo/".
In this case, you should ignore redundant slashes and return "/home/foo".
【解題思路】
1)、..表示返回上級目錄,.沒有任何意義。
2)、用棧去處理,每次拿到/和/之間的字串,如果是.,不處理,如果是..,彈出棧頂元素,表示回到上級目錄。如果是其他字串,說明是個目錄,入棧即可。
不斷的入棧和出棧,如果棧元素為空,說明在根目錄,直接輸出/,否則依次輸出目錄,注意棧底元素為第一級目錄。

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;
    }
}