[Leetcode] [Java] Simplify Path

Source: Internet
Author: User

Title:

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/" .
    Should ignore redundant slashes and return "/home/foo" .

Test Instructions:

Given an absolute path (Unix-style), simplify it.

Like what:

Path = "/home/", ="/home"
Path = "/a/./b/../../c/", ="/c"

Individual cases:

1. Have you considered the example path = "/../" ? Then you need to return "/" .

2. Another particular situation is the duplication ‘/‘ , for example "/home//foo/" . Then you need to return to the repeating section "/home/foo" .

Algorithm Analysis:

* The idea is more clear, is to maintain a stack, for each block (with/as the demarcation) to analyze,
* If you encounter a. is to go to the next level, then it is the stack operation,
* If you encounter a. Then stay current, skip directly,
* Other file paths are directly in the stack.
* Finally, based on the contents of the stack into a path (here is the stack to the array, and then add).
* When all the separated strings have been processed, check if the first stack is empty and if the stack is empty, it proves that there is no directory name that can be rebuilt and returns "/".
* When the first stack is not empty, we need to restore the path. So here I apply the second stack, first stack elements in the stack to the second stack, and then use the second stack to restore back to the original path

AC Code:

public class Solution {public string Simplifypath (string path) {if (Path = = null| |                Path.length () ==0) return path;        stack<string> stack = new stack<string> ();                string[] List = Path.split ("/"); for (int i=0; i<list.length; i++) {if (List[i].equals (")") | |            List[i].length () ==0)//If you encounter a. is stay current, skip directly over continue; else if (!list[i].equals (".."))            Other file paths can be stack.push directly into the stack (list[i]); else {if (!stack.isempty ())//If encountered:            is to go up a layer, then is to do the stack Operation Stack.pop ();                }} StringBuilder res = new StringBuilder ();        stack<string> temp = new stack<string> (); while (!stack.isempty ()) Temp.push (Stack.pop ()),//The original path is/a/b/c/, the order of the stack is: a b C, if the stack is restored in turn:/c/b/a (Error!).                ), the correct answer is:/a/b/c while (!temp.isempty ()) Res.append ("/" +temp.pop ()); IfRes.length () ==0)//If the stack is empty, it proves that there is no directory name that can be rebuilt, and returns "/" to Res.append ("/");    return res.tostring (); }}


Copyright NOTICE: This article is the original article of Bo Master, reprint annotated source

[Leetcode] [Java] Simplify Path

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.