Leetcode-simplify Path

Source: Internet
Author: User

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.

Solution:

1  Public classSolution {2      Publicstring Simplifypath (string path) {3         if(Path.isempty ())return"";4 5StringBuilder Builder =NewStringBuilder ();6Builder.append ('/');7         intindex = 1;8          while(index<path.length ()) {9             //get the Next Directory command.Ten             intNext =index; One              while(Next<path.length () && Path.charat (next)! = '/') next++; AString dir =path.substring (index,next); -             if(Dir.equals (".") | |Dir.isempty ()) { -index = next+1; the                 Continue; -}Else if(Dir.equals (".."))){ - Pathback (builder); -}Else { + Builder.append (dir); -Builder.append ('/'); +             } Aindex = next+1; at         } -  -         if(Builder.length () >1 && Builder.charat (Builder.length ()-1) = = '/') -Builder.deletecharat (Builder.length ()-1); -         returnbuilder.tostring (); -     } in  -      Public voidpathback (StringBuilder builder) { toBuilder.deletecharat (Builder.length ()-1); +          while(Builder.length () >0 && Builder.charat (Builder.length ()-1)! = '/') -Builder.deletecharat (Builder.length ()-1); the         if(Builder.length () ==0) builder.append ('/'); *     } $}

Leetcode-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.