Leetcode_71_Simplify Path, pathsumleetcode
Please help me increase your popularity. If you have any errors or questions, please leave a message to correct them. Thank you.
Simplify Path
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 shoshould return "/".
Another corner case is the path might contain multiple slashes '/'Together, such as "/home // foo /".
In this case, you shoshould ignore redundant slashes and return "/home/foo ".
Analysis of ideas:
Be sure to stay awake in the face of string questions. Analyze the questions before starting the code.
The requirement of the question is to output the simplest path in Unix. the root directory of Unix files is "/", "." indicates the current directory, and "..." indicates the upper-level directory.
For example:
Input 1:
/../A/B/c /./..
Output 1:
/A/B
Simulate the entire process:
1. "/" root directory
2. ".." Jump to the parent directory. The parent directory is empty, so it is still in "/"
3. "a" Enter subdirectory a, which is currently in "/"
4. "B" enters subdirectory B, which is currently in "/a/B"
5. "c" enters the subdirectory c, which is currently in "/a/B/c"
6. "." Current Directory, not operated, still in "/a/B/c"
7. ".." return to the parent directory, which is "/a/B"
Implementation Method: Use a stack to simulate the action of the path. When ".." is not operated, and ".." is rolled back, all other situations are pushed into the stack.
// Vs2012 test code # include <iostream> # include <stack> # include <string> using namespace std; class Solution {private: void pathToDirectories (stack <string> & directories, string & path) {string name; name. clear (); path = path + '/'; for (int I = 0; I <path. length (); I ++) {if (path [I] = '/') {if (! Name. empty () if (name [0] = '. '& name. length () = 1) name. clear (); else if (name [0] = '. '& name. length () = 2 & name [1] = '. ') {if (! Directories. empty () directories. pop (); name. clear ();} else {directories. push (name); name. clear () ;}} elsename = name + path [I] ;}} void directoriesToPath (string & ans, stack <string> & directories) {ans. clear (); while (! Directories. empty () {ans = directories. top () + '/' + ans; directories. pop ();} if (! Ans. empty () ans = ans. substr (0, ans. length ()-1); // substr method: returns a substring with a specified length starting from the specified position. Ans = '/' + ans;} public: string simplifyPath (string path) {stack <string> directories; pathToDirectories (directories, path); string ans; directoriesToPath (ans, directories); return ans ;}}; int main () {string path; getline (cin, path); // or cin >>s; Solution lin; cout <lin. simplifyPath (path) <endl; return 0 ;}
// Method 1: Self-Test Acceptedclass Solution {private: void pathToDirectories (stack <string> & directories, string & path) {string name; name. clear (); path = path + '/'; for (int I = 0; I <path. length (); I ++) {if (path [I] = '/') {if (! Name. empty () if (name [0] = '. '& name. length () = 1) name. clear (); else if (name [0] = '. '& name. length () = 2 & name [1] = '. ') {if (! Directories. empty () directories. pop (); name. clear ();} else {directories. push (name); name. clear () ;}} elsename = name + path [I] ;}} void directoriesToPath (string & ans, stack <string> & directories) {ans. clear (); while (! Directories. empty () {ans = directories. top () + '/' + ans; directories. pop ();} if (! Ans. empty () ans = ans. substr (0, ans. length ()-1); // substr method: returns a substring with a specified length starting from the specified position. Ans = '/' + ans;} public: string simplifyPath (string path) {stack <string> directories; pathToDirectories (directories, path); string ans; directoriesToPath (ans, directories); return ans ;}};