zoukankan      html  css  js  c++  java
  • Simplify Path

    all the possible

     1 public class Solution {
     2     public String simplifyPath(String path) {
     3         // IMPORTANT: Please reset any member data you declared, as
     4         // the same Solution instance will be reused for each test case.
     5         Stack<String> mystack = new Stack<String>();
     6         char[] mystring = path.toCharArray();
     7         for(int i=0; i<mystring.length; i++)
     8         {
     9             if(mystring[i] == '/' && i + 1 < mystring.length && mystring[i+1] == '.'&&i + 2 < mystring.length && mystring[i+2] == '.')
    10             {
    11                 i+=2;
    12                 if(!mystack.isEmpty())
    13                     mystack.pop();
    14             }
    15             else if(mystring[i] == '/' && i + 1 < mystring.length && mystring[i+1] == '.'&&i + 2 < mystring.length && mystring[i+2] == '/')
    16             {
    17                 i+=1;
    18             }
    19             
    20             else if(mystring[i] == '/' && i + 1 < mystring.length && mystring[i+1] == '.' && i + 2 < mystring.length && isChar(mystring[i+2])){
    21                 int count = 2;
    22                 StringBuilder tmp = new StringBuilder();
    23                 tmp.append('.');
    24                 while((i+count) < mystring.length && isChar(mystring[i+count])){
    25                     tmp.append(mystring[i+count]);
    26                     count++;
    27                 }
    28                 mystack.push(tmp.toString());
    29                 i+=(count-1);
    30             }
    31             
    32             else if(mystring[i] == '/' && i + 1 < mystring.length && mystring[i+1] == '/')
    33                 continue;
    34             else if(mystring[i] == '/' && i + 1 < mystring.length && isChar(mystring[i+1])){
    35                 int count = 1;
    36                 StringBuilder tmp = new StringBuilder();
    37                 while((i+count) < mystring.length && isChar(mystring[i+count])){
    38                     tmp.append(mystring[i+count]);
    39                     count++;
    40                 }
    41                 mystack.push(tmp.toString());
    42                 i+=(count-1);
    43             }
    44         }
    45         if(mystack.isEmpty())
    46             return "/";
    47         return print(mystack);
    48     }
    49     
    50     private String print(Stack<String> mystack)
    51     {
    52         if(mystack.isEmpty())
    53             return "";
    54         String tmp = mystack.pop();
    55         return print(mystack) + "/" + tmp;
    56     }
    57     private boolean isChar(char mychar){
    58         if((mychar >= 'a' && mychar <= 'z') || mychar == '_' || (mychar >= '0' && mychar <= '9') || (mychar >= 'A' && mychar <= 'Z'))
    59             return true;
    60         return false;
    61     }
    62 }
  • 相关阅读:
    CentOS6.5配置MySQL主从同步
    CentOS6.5安装telnet
    linux 下安装Google Chrome (ubuntu 12.04)
    jdk w7环境变量配置
    JDBCConnectionException: could not execute query,数据库连接池问题
    注意开发软件的版本问题!
    linux mysql命令行导入导出.sql文件 (ubuntu 12.04)
    linux 下root用户和user用户的相互切换 (ubuntu 12.04)
    linux 下 vim 的使用 (ubuntu 12.04)
    linux 下安装配置tomcat-7 (ubuntu 12.04)
  • 原文地址:https://www.cnblogs.com/jasonC/p/3430597.html
Copyright © 2011-2022 走看看