【九度】题目1090:路径打印 && 【LeetCode】Simplify Path

1、九度 题目1090:路径打印
时间限制: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
****************************************************************/
2、LeetCode Simplify Path 

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


你可能感兴趣的:(【九度】题目1090:路径打印 && 【LeetCode】Simplify Path)