【LeetCode】C# 71、Simplify Path
来源:互联网 发布:如何下载excel软件 编辑:程序博客网 时间:2024/06/06 00:42
Given an absolute path for a file (Unix-style), simplify it.
For example,
path = “/home/”, => “/home”
path = “/a/./b/../../c/”, => “/c”
简化IP地址。
思路:利用string.Split来吧字符串分成字符串组,然后通过堆栈来存放有效名称。遇到/../则后退一格, /./则无视,//也无视。最后别忘了/../这种特殊情况。要返回/。
public class Solution { public string SimplifyPath(string path) { string[] arr = path.Split('/'); Stack<string> stack = new Stack<string>(); for (int i = 0; i < arr.Length; i++){ if (arr[i] != ""){ if (arr[i] == ".."){ if (stack.Count > 0) stack.Pop(); } else if (arr[i] == ".") { } else { stack.Push(arr[i]); } } } StringBuilder sb = new StringBuilder(); while (stack.Count > 0){ sb.Insert(0, "/" + stack.Pop()); } //solve corner case like "/../" if (sb.Length == 0) sb.Append("/"); return sb.ToString(); }}
阅读全文
0 0
- 【LeetCode】C# 71、Simplify Path
- LeetCode 71 Simplify Path
- LeetCode(71) Simplify Path
- [leetcode 71] Simplify Path
- leetcode || 71、Simplify Path
- LeetCode(71) Simplify Path
- leetcode 71: Simplify Path
- 【leetcode】【71】Simplify Path
- leetcode 71 Simplify Path
- LeetCode 71 Simplify Path
- Leetcode 71 Simplify Path
- leetcode-71 simplify path
- LeetCode #71 Simplify Path
- Leetcode NO.71 Simplify Path
- [leetcode-71]Simplify Path(C)
- LeetCode(71) Simplify Path
- LeetCode 71 Simplify Path (栈)
- 【Leetcode】71 Simplify path 简化路径
- LibreOJ6162(美团 CodeM 初赛 Round A)[身体训练]--期望
- Spring boot 整合 tk mybatis 插件
- 只调整指针来交换两个相邻的元素【双链表】
- 小鹏学长经验之谈
- 指针和lcd1602的认识
- 【LeetCode】C# 71、Simplify Path
- Educational Codeforces Round 30 Balanced Substring
- 【SQL Server学习笔记】23:部分可编程对象
- 关于java项目中System.out.println()标准输出方法性能影响分析
- Java
- Java 打印流PrintStream
- for编写输出等腰三角形,菱形
- Maven的常用命令
- 连续总结第四天