Leecode71

Leecode71

path在split之后的数组中有空字符串,大坑

	import java.util.Stack;

//存在空串
public class Solution {
    public static void main(String[] args) {
//        Stack<String> stack = new Stack<>();
//        String str = "/a/./b///../c/../././../d/..//../e/./f/./g/././//.//h///././/..///";
//        String[] strs = str.split("/");
//        for (String s : strs) {
//            System.out.print(s + " ");
//        }
//        for (String s : strs) {
//            stack.push(s);
//        }
//        String ans = String.join("/",stack);
//        System.out.println();
//        System.out.println(ans);
        String str = "/a/./b///../c/../././../d/..//../e/./f/./g/././//.//h///././/..///";

        String ans = new Solution().simplifyPath(str);
        System.out.println("ans:"+ans);
    }

    public String simplifyPath(String path) {
        Stack<String> stack = new Stack<>();

        String[] strs = path.split("/");
//        System.out.println("------before------");
//        for (String str : strs) {
//            System.out.print(str);
//        }
//        System.out.println();
        for (String str : strs) {
            if(str.equals("..")){
                if(!stack.isEmpty())
                    stack.pop();
                //还需要去除空格
            } else if(!str.equals(".") && !str.equals("")){
                stack.push(str);
            }
        }
//        System.out.println("-----after-------");
//        for (String s : stack) {
//            System.out.print(s);
//        }
//        System.out.println();
        return "/" + String.join("/",stack);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值