Leetcode - Simplify Path

本文介绍了一个算法用于简化Unix风格文件路径。通过解析路径字符串并利用栈来存储有效的目录部分,该算法能有效处理.和..等特殊符号,最终返回规范化的最简路径。

Question:

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

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

===========================================================================================================

We need to know:

  • "." current working directory  /a /. / the command does not direct to any other directories. Basically it does nothing
  • ".." jump one level back  / c / a / .. / the current working directory is "a", if we do ".." after "a", then we jump one level back to "c"
  • "/" this is the divider of commands, which means "///" no matter how many grouped together, it is just " / ".
  • other than the previous three mentioned, they are all directory names. Those directories are the ones we need to store

Because reading the commands starts from left to right, it makes sense that we will need to loop through the command line and evaluate the meaning of each command. As we will need print out the meaningful path, we need to store each meaningful command, so we use stack. It makes becuase when we have "..", we need to know previous directory and delete it. Stack will allow us to do it this way easily.

 1 class Solution {
 2     public String simplifyPath(String path) {
 3         String[] strs = path.split("/");
 4         //create a stack to store meaningful command
 5         Stack<String> stack = new Stack<>();
 6         for (int i = 0; i < strs.length; i++) {
 7             //jump one directory back
 8             if (strs[i].equals("..")) {
 9                 if (!stack.isEmpty()) {
10                     stack.pop();
11                 }
12             } else if ((!strs[i].equals(".")) && (!strs[i].equals(""))) {
13                 stack.push(strs[i]);
14             }
15             //if the command is . or empty, then the command is not meaningful, so we will skep the execution
16         }
17         //print out the path
18         path = "";
19         while (!stack.isEmpty()) {
20             path = "/" + stack.pop() + path;
21         }
22         if (path.length() > 0) {
23             return path;
24         }
25         return "/";
26     }
27 }

 

 

 

 

转载于:https://www.cnblogs.com/shuaih/p/7435861.html

dnSpy是目前业界广泛使用的一款.NET程序的反编译工具,支持32位和64位系统环境。它允许用户查看和编辑.NET汇编和反编译代码,以及调试.NET程序。该工具通常用于程序开发者在维护和调试过程中分析程序代码,尤其在源代码丢失或者无法获取的情况下,dnSpy能提供很大的帮助。 V6.1.8版本的dnSpy是在此系列软件更新迭代中的一个具体版本号,代表着该软件所具备的功能与性能已经达到了一个相对稳定的水平,对于处理.NET程序具有较高的可用性和稳定性。两个版本,即32位的dnSpy-net-win32和64位的dnSpy-net-win64,确保了不同操作系统架构的用户都能使用dnSpy进行软件分析。 32位的系统架构相较于64位,由于其地址空间的限制,只能支持最多4GB的内存空间使用,这在处理大型项目时可能会出现不足。而64位的系统能够支持更大的内存空间,使得在处理大型项目时更为方便。随着计算机硬件的发展,64位系统已经成为了主流,因此64位的dnSpy也更加受开发者欢迎。 压缩包文件名“dnSpy-net-win64.7z”和“dnSpy-net-win32.7z”中的“.7z”表示该压缩包采用了7-Zip压缩格式,它是一种开源的文件压缩软件,以其高压缩比著称。在实际使用dnSpy时,用户需要下载对应架构的压缩包进行解压安装,以确保软件能够正确运行在用户的操作系统上。 dnSpy工具V6.1.8版本的发布,对于.NET程序员而言,无论是32位系统还是64位系统用户,都是一个提升工作效率的好工具。用户可以根据自己计算机的操作系统架构,选择合适的版本进行下载使用。而对于希望进行深度分析.NET程序的开发者来说,这个工具更是不可或缺的利器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值