leetcode #71 in cpp

原创 2016年05月31日 09:45:25

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

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

Solution:

Rules could be found in wiki. 

1. when we meet "..", delete its adjacent left folder

2. when we meet ".", do nothing. Just skip it. 

3. when we meet '/', it means we meet a new folder or the end of current folder.

Code:

class Solution {
public:
    string simplifyPath(string path) {
        vector<string> stk;
        int start_ind = -1;
        
        int i = 0;
        while(i<path.size()){
            //collect strings between '/'  and '/'
            while(i<path.size() &&path[i] == '/' ) i++;//skip leading /
            if(i == path.size()) break;
                start_ind = i;
            while(i<path.size() &&path[i] != '/' ) i++;//get the following characters
            i--;
            
            //get substring
            string s = path.substr(start_ind, i - start_ind+1);
            if(s == ".") {//if it is "".", do nothing
                i++;
                continue;
            } 
            else if(s == ".."){//if it is "..", we need to delete its left
                    if(!stk.empty()) stk.pop_back();
                }else{
                    stk.push_back(s);//if it is a path, push it.
                }
            i++;   
        }
        if(stk.empty()) return "/";
        string res = "";
        for(int i = 0; i < stk.size(); i ++){
            res+="/"+stk[i];
        }
        return res;
                        
            
        }
        
};


【LeetCode-面试算法经典-Java实现】【215-Kth Largest Element in an Array(数组中第K大的数)】

【215-Kth Largest Element in an Array(数组中第K大的数)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】代码下载【https://github...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月28日 06:57
  • 4100

V4L2中命令在内核中来龙去脉

1. VIDIOC_REQBUFS: 请求Kernel分配Video Buffer 其申请流程如下图所示: 1.1  Kernel态相关数据结构 [cpp] view pla...
  • zhou13454069844
  • zhou13454069844
  • 2013年11月18日 14:41
  • 1984

Linux设备驱动篇——[I2C设备驱动-1]

Linux 设备驱动篇之I2c设备驱动 fulinux 一、I2C驱动体系 虽然I2C硬件体系结构和协议都很容易理解,但是Linux I2C驱动体系结构却有相当的复杂度,它主要由3部分组成,...
  • luoyouren
  • luoyouren
  • 2015年04月24日 12:13
  • 1095

leetcode #114 in cpp

Given a binary tree, flatten it to a linked list in-place. For example, Given 1 ...
  • xiaolienahu
  • xiaolienahu
  • 2016年06月18日 22:12
  • 66

leetcode #160 in cpp

Write a program to find the node at which the intersection of two singly linked lists begins. F...
  • xiaolienahu
  • xiaolienahu
  • 2016年06月30日 10:33
  • 135

Leetcode 285. Inorder Successor in BST (Medium) (cpp)

Leetcode 285. Inorder Successor in BST (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月28日 03:38
  • 262

leetcode #139 in cpp

Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separa...
  • xiaolienahu
  • xiaolienahu
  • 2016年06月25日 11:08
  • 68

leetcode #86 in cpp

Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr...
  • xiaolienahu
  • xiaolienahu
  • 2016年06月12日 11:37
  • 76

leetcode #24 in cpp

The question is to swap each pair in the linked list. And the requirement is to use constant space. ...
  • xiaolienahu
  • xiaolienahu
  • 2016年05月23日 04:14
  • 77

Leetcode 450. Delete Node in a BST (Medium) (cpp)

Leetcode 450. Delete Node in a BST (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月26日 07:50
  • 294
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #71 in cpp
举报原因:
原因补充:

(最多只允许输入30个字)