zoukankan      html  css  js  c++  java
  • simplify-path

    https://leetcode.com/problems/simplify-path/

    https://leetcode.com/mockinterview/session/result/xjl3d3m/

    现在字符串比较,都知道用 equals了,哈哈。不能用==,否则可能有bug。

    package com.company;
    
    
    import java.util.*;
    
    class Solution {
        public String simplifyPath(String path) {
            Stack<String> stk= new Stack<>();
            String[] strs = path.split("/");
            for (int i=0; i<strs.length; i++) {
                if (strs[i].equals("") || strs[i].equals(".")) {
                    continue;
                }
                else if (strs[i].equals("..")) {
                    if (!stk.empty()) {
                        stk.pop();
                    }
                }
                else {
                    stk.push(strs[i]);
                }
            }
    
            Stack<String> newStk= new Stack<>();
            String tmp;
            while (!stk.empty()) {
                tmp = stk.pop();
                newStk.push(tmp);
            }
    
            StringBuilder sb = new StringBuilder();
            while (!newStk.empty()) {
                sb.append("/" + newStk.pop());
            }
            String str = sb.toString();
            if (str.equals("")) {
                str = "/";
            }
            return str;
        }
    }
    
    public class Main {
    
        public static void main(String[] args) throws InterruptedException {
    
            System.out.println("Hello!");
            Solution solution = new Solution();
    
            // Your Codec object will be instantiated and called as such:
            String path = "/a/./b/../../c/";
            String ret = solution.simplifyPath(path);
            System.out.printf("ret:%s
    ", ret);
    
            System.out.println();
    
        }
    
    }
  • 相关阅读:
    。。
    前端jQuery选择器
    杂物柜。。。。
    (转载)pycharm相关问题
    http协议基础 2.HTTP的三次握手,四次挥手
    http协议基础 1.TCP/IP五层模型 与OSI七层协议
    yum无法使用解决方法(比较全,以后如果遇到别的问题还会添加)
    3
    2
    1
  • 原文地址:https://www.cnblogs.com/charlesblc/p/6033695.html
Copyright © 2011-2022 走看看