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 the case where path = "/../"?

In this case, you should return "/".

Another corner case is the path might contain multiple slashes ‘/‘ together, such as "/home//foo/".

In this case, you should ignore redundant slashes and return "/home/foo".

思路:此题是有些坑爹的,原因就是...的类型也算合法,其路径格式不是windows,是Linux的,对于Linux小白来说,路径还是不好考虑的。

在解的过程中,如果是对字符遍历将会有些麻烦,最简单的办法就是对字符串按“/”分割数组,然后按情况操作。具体代码如下:

public class Solution {
    public String simplifyPath(String path) {
    	//将//都简化成/
        path = path.replaceAll("/{2,}","/");
        System.out.println(path);
        Stack<String> st = new Stack<String>();
        //按/分割数组
    	String[] p = path.split("/");
    	for(int i = 0; i < p.length; i++){
    		//..表示后退一个路径
    		if(p[i].equals("..")){
    			 if(!st.isEmpty())//不为空才后退
    				 st.pop();
    		}
    		//.忽视,表示当前路径
    		else if(!p[i].equals(".") ){
    			st.push(p[i]);
    		}
    	}
        //现在栈里的就是简化的路径
        String s = "";
        while(!st.isEmpty()){
            s = "/" + st.pop() + s;//先进后出
        }
        s = "/" + s;//补上开头的/
        if(s.length() > 1)
        	s = s.replaceAll("/$","").replaceAll("/{1,}", "/");//去除结尾的/
        return s;
    }
}

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

时间: 2024-12-27 18:54:05

leetCode 71.Simplify Path(化简路径) 解题思路和方法的相关文章

leetCode 64.Minimum Path Sum (最短路) 解题思路和方法

Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 思路:此题和前面几个机器人的题非常相像,只是变化了一点,具体代码和

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 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

[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

leetCode 36.Valid Sudoku(有效的数独) 解题思路和方法

Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be partially filled, where empty cells are filled with the character '.'. A partially filled sudoku which is valid. Note: A valid Sudoku boa

leetCode 47.Permutations II (排列组合II) 解题思路和方法

Permutations II Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example, [1,1,2] have the following unique permutations: [1,1,2], [1,2,1], and [2,1,1]. 思路:这题相比于上一题,是去除了反复项. 代码上与上题略有区别.详细代码例如以下

leetCode 35.Search Insert Position (搜索插入位置) 解题思路和方法

Search Insert Position Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the array. Here are few examples. [1,3,5

leetCode 20.Valid Parentheses (有效的括号) 解题思路和方法

Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" a

leetCode 45.Jump Game II (跳跃游戏) 解题思路和方法

Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum nu