LeetCode71 Simplify Path java题解

题目:

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

For example,

path = "/home/", => "/home"

path = "/a/./b/../../c/", => "/c"

题解:

解题思路:这题是简化目录,每一级目录前面都有一个斜杠,我可以首先对斜杠进行分割,分割之后得到的结果无外乎4种情况:正常目录名称,空字符,".",".."。对于“.”和空字符我们忽略它,对于正常的目录名称我们直接压入栈,对于"..",我们把栈顶元素出栈。通过这些处理之后,如果栈为空我们就返回根目录,如果栈不为空我们就逐个出栈并在每个元素前面加一个斜杠,最先出的位于路径的最后面。

代码:

import java.sql.Array;
import java.util.Stack;

public class LeetCode71_SimplifyPath {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String input="/a/./b/../../c/";
		System.out.println(simplifyPath(input));

	}

	public static String simplifyPath(String path) {

		String[] pathArray=path.split("/");
		int length=pathArray.length;
		Stack< String> stack=new Stack<>();
		String result="";
		for(int i=0;i<length;i++)
		{
			if(pathArray[i].equals("")||pathArray[i].equals("."))
			{}
			else if(pathArray[i].equals(".."))
			{
				if(!stack.isEmpty())
				{
				stack.pop();
				}

			}
			else {
				stack.push(pathArray[i]);
			}
		}
		if(stack.isEmpty())
			return "/";
		while(!stack.isEmpty())
		{
		result="/"+stack.pop()+result;
		}

		return result;

    }

}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-25 19:53:39

LeetCode71 Simplify Path java题解的相关文章

leetcode 71 Simplify Path ------ java

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case where p

Simplify Path leetcode java

题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" Corner Cases: Did you consider the case where path = "/../"

[C++]LeetCode: 117 Simplify Path (简化Unix路径 list双向链表)

题目: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider the case w

[LeetCode] 71. Simplify Path 简化路径

Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" Corner Cases: Did you consider the case where path = "/../"?In th

71. Simplify Path做题报告

题目链接: 71. Simplify Path 题目大意: 简化路径,如果对于‘.'字符,表明为此目录,对于‘..'字符,表明为回到此目录上一节点(即:删除上一节点),我们现在需要删除多余的'/'字符和'.'字符 做题报告: (1)该题涉及的算法与数据结构与知识点 Java NIO中的Files类,正则表达式,栈 (2)自己的解答思路+代码+分析时间和空间复杂度 栈 class Solution { public String simplifyPath(String path) { String

leetcode 71 Simplify Path

题目连接 https://leetcode.com/problems/simplify-path/ Simplify Path Description Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

【LeetCode】Simplify Path

Simplify Path Given an absolute path for a file (Unix-style), simplify it. Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"

[leetcode]Simplify Path @ Python

原题地址:https://oj.leetcode.com/problems/simplify-path/ 题意: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c" click to show corn

leetCode 71.Simplify Path(化简路径) 解题思路和方法

Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", => "/home" path = "/a/./b/../../c/", => "/c" click to show corner cases. Corner Cases: Did you consider