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

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

核心在于编写一个split函数以及用进出栈来保存最简路径。


class Solution
{
public:
// true if the argument is slash, false otherwise
static bool is_slash(char c)
{
return (c==‘/‘);
}

// false if the argument is slash, true otherwise
static bool not_slash(char c)
{
return !is_slash(c);
}

vector<string> split(const string& str)
{
typedef string::const_iterator iter;
vector<string> ret;

iter i = str.begin();
while (i != str.end()) {

// ignore leading slashes
i = find_if(i, str.end(), not_slash);

// find end of next word
iter j = find_if(i, str.end(), is_slash);

// copy the characters in [i, j)
if (i != str.end())
ret.push_back(string(i, j));
i = j;
}
return ret;
}

string simplifyPath(string path)
{
stack<string> pstack;
vector<string> pv = split(path);
for(vector<string>::size_type st = 0; st < pv.size(); st ++)
{
if(pv[st] == "..")
{
if(!pstack.empty())
pstack.pop();
}
else if(pv[st] != ".")
pstack.push(pv[st]);
}

string output = "";
if(pstack.empty())
{
output = "/";
return output;
}
while(!pstack.empty())
{
output = "/" + pstack.top() + output;
pstack.pop();
}
return output;
}
};

【LeetCode】Simplify Path,布布扣,bubuko.com

时间: 2024-12-22 16:41:23

【LeetCode】Simplify Path的相关文章

【LeetCode】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

【Leetcode】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. 思路:简单的动态规划题目,设f(m, n)为从(0, 0)到达(m

【LeetCode】113. Path Sum II 基于Java和C++的解法及分析

113. Path Sum II Total Accepted: 80509 Total Submissions: 284188 Difficulty: Medium Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8

【LeetCode】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】112 - Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 return true, as t

【leetcode】Minimum Path Sum(easy)

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】 Unique Path ||(easy)

Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty space is marked as 1 and 0 respectively in the grid. For example, There is one obstacle in the middl

【LeetCode】栈 stack(共40题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [20]Valid Parentheses [42]Trapping Rain Water [71]Simplify Path [84]Largest Rectangle in Histogram [85]Maximal Rectangle [94]Binary Tree Inorder Traversal [103]Binary Tree Zigzag Level

【LeetCode】字符串 string(共112题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [3]Longest Substring Without Repeating Characters [5]Longest Palindromic Substring [6]ZigZag Conversion [8]String to Integer (atoi) [10]Regular Expression Matching [12]Integer to Roman