zoukankan      html  css  js  c++  java
  • leetcode Simplify Path

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

    For example,
    path = "/home/", => "/home"
    path = "/a/./b/../../c/", => "/c"

    1,java里string的函数split可以用来拆分string。拆分之后放入数组里

    2,stringbuffer 的insert功能

    3. 记得string 只能用equal

    public class Solution {
        public String simplifyPath(String path) {
             Stack<String> store= new Stack<String>();
            StringBuffer result=new StringBuffer();
            String[] temp=path.split("/");
            for(int i=0;i<temp.length;i++){
            	if(temp[i].equals(".")){
            		continue;
            	}
            	else if(temp[i].equals("..")){
            		if(!store.isEmpty()){
            			store.pop();
            		}
            	}
            	else if(!temp[i].isEmpty()){
            		store.push(temp[i]);
            	}
            }
            while(!store.isEmpty()){
            	result.insert(0,"/"+store.pop());        
            }
            if(result.length()==0){
                 result.append("/");
            }
            return result.toString();
        }
    }
    

      

  • 相关阅读:
    b站尚硅谷MySQL笔记(婷姐初级,周阳高级)
    word--公式添加编号
    excel--长数字显示问题
    R语言--蒙特卡洛计算定积分
    数学
    数学
    Computer Science
    Computer Science
    Computer Science
    元学习
  • 原文地址:https://www.cnblogs.com/lilyfindjobs/p/4100946.html
Copyright © 2011-2022 走看看