原题地址

用栈保存化简后的路径。把原始路径根据"/"切分成若干小段,然后依次遍历

若当前小段是"..",弹栈

若当前小段是".",什么也不做

否则,入栈

代码:

 string simplifyPath(string path) {
   vector<string> buffer;
   char *tok = NULL;

   tok = strtok((char *) path.c_str(), "/");
   while (tok) {
     cout << tok << endl;
     ) {
       if (!buffer.empty())
         buffer.pop_back();
     }
     )
       buffer.push_back(string(tok));
     tok = strtok(NULL, "/");
   }

   string res;
   for (auto dir : buffer)
     res += "/" + dir;
   return buffer.empty() ? "/" : res;
 }

Leetcode#71 Simplify Path的更多相关文章

  1. 71. Simplify Path(M)

    71. Simplify Path Given an absolute path for a file (Unix-style), simplify it. For example, path = & ...

  2. 【LeetCode】71. Simplify Path

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

  3. 【一天一道LeetCode】#71. Simplify Path

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  4. 【leetcode】Simplify Path

    题目简述: Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/& ...

  5. Java for LeetCode 071 Simplify Path

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

  6. 71. Simplify Path

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

  7. Leetcode 之Simplify Path @ python

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

  8. 71. Simplify Path (Stack)

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

  9. 71. Simplify Path压缩文件的绝对路径

    [抄题]: Given an absolute path for a file (Unix-style), simplify it. For example,path = "/home/&q ...

随机推荐

  1. retire or not retire ? is a question.

    corejava 上的一段代码 因吹思婷 "C:\Program Files\Java\jdk1.8.0_101\bin\java" -Didea.launcher.port=75 ...

  2. DDoS-Deflate安装及配置

    (D)DoS-Deflate是一款免费的用来防御和减轻DDoS攻击.它通过netstat监测跟踪创建大量网络连接的IP地址,在检测到某个结点超过预设的限制时,该程序会通过APF或iptables禁止或 ...

  3. 项目整体说明及WBS练习

    乐谱识别研究的意义 纸质乐谱向数字化乐谱的转换,是人类音乐活动与计算机音乐处理之间进行信息交流的必经之路.光学乐谱识别是将纸质乐谱扫描输入计算机后,对乐谱图像加以处理.识别.分析,最终获得乐谱的计算机 ...

  4. Nginx负载均衡使用ip

    upstream test1{ server 192.168.1.213; server 192.168.1.37; } server { listen 80; # default backlog=2 ...

  5. 【Spark篇】---SparkStreaming算子操作transform和updateStateByKey

    一.前述 今天分享一篇SparkStreaming常用的算子transform和updateStateByKey. 可以通过transform算子,对Dstream做RDD到RDD的任意操作.其实就是 ...

  6. 【进阶1-3期】JavaScript深入之内存空间详细图解(转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记 https://mp.weixin.qq.com/s/x4ZOYysb9XdT1grJbBMVkg 今天介绍的是JS内存空间,了解内存空间中的堆和 ...

  7. JavaScript 隐式类型转换

    JavaScript 隐式类型转换 原文:https://blog.csdn.net/itcast_cn/article/details/82887895 · 1.1 隐式转换介绍 · 1.2 隐式转 ...

  8. [c/c++] programming之路(3)、转义字符及内存

    一.转义字符 #include<stdio.h> void main(){ printf("hello \nworld\a");// \n换行 \a机器响一声 getc ...

  9. [osg]OSG使用更新回调来更改模型

    使用回调类实现对场景图形节点的更新.本节将讲解如何使用回调来实现在每帧的更新遍历(update traversal)中进行节点的更新.        回调概览       用户可以使用回调来实现与场景 ...

  10. HTML5 Canvas ( 扩展context(&#39;2d&#39;) ) CanvasRenderingContext2D.prototype.你的方法名

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...