原题地址

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

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

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

否则,入栈

代码:

 string simplifyPath(string path) {
vector<string> buffer;
char *tok = NULL; tok = strtok((char *) path.c_str(), "/");
while (tok) {
cout << tok << endl;
if (strcmp(tok, "..") == ) {
if (!buffer.empty())
buffer.pop_back();
}
else if (strcmp(tok, ".") != )
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. [LeetCode] 71. Simplify Path 简化路径

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

  2. 【LeetCode】71. Simplify Path 解题报告(Python)

    [LeetCode]71. Simplify Path 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  3. 71. Simplify Path(M)

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

  4. 【LeetCode】71. Simplify Path

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

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

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

  6. 【leetcode】Simplify Path

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

  7. Java for LeetCode 071 Simplify Path

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

  8. 71. Simplify Path

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

  9. Leetcode 之Simplify Path @ python

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

随机推荐

  1. Android Material Design:NavigationView抽屉导航菜单

    需要添加的包: 测试代码: package com.zzw.navigationview; import android.app.Activity; import android.os.Bundle; ...

  2. HDFS文件读写流程

    一.HDFS HDFS全称是Hadoop Distributed System.HDFS是为以流的方式存取大文件而设计的.适用于几百MB,GB以及TB,并写一次读多次的场合.而对于低延时数据访问.大量 ...

  3. 【easyui】--普通js中获取easyui中分页信息(page,pageSize等)

    对于datagrid,获取其分页信息: 方法: var pageopt = $('#list_data').datagrid('getPager').data("pagination&quo ...

  4. wing IDE破解方法

    WingIDE是我接触到最好的一款Python编译器了.但其属于商业软件,注册需要一笔不小的费用.因此,这篇简短的文章主要介绍了破解WingIDE的方法.破解软件仅供学习或者教学使用,如果您是商业使用 ...

  5. 内存详解--理解 JVM 如何使用 AIX 上的本机内存

    转自---http://www.ibm.com/developerworks/cn/java/j-nativememory-aix/

  6. Unix 编程

    1. Users and Groups 真实用户ID和真实组ID 真实用户ID和组ID表示运行进程的真实用户 ID 和 组ID. 有效用户ID和有效组IDp 有效 ID 是进程进行相关操作(比如系统调 ...

  7. Ubuntu下编译内核

    一.下载源代码和编译软件的准备 下载内核源代码:http://www.kernel.org/ 注意,点击2.6.25内核的F版,即完整版. 如果你懒得去网站点联接,运行下列命令:  代码: $cd ~ ...

  8. TextSwitcher,译为文字转换器控件

    ViewSwitcher仅仅包含子类型TextView.TextSwitcher被用来使屏幕上的label产生动画效果.每当setText(CharSequence)被调用时,TextSwitcher ...

  9. Ubuntu14.04 Objective-C hello world

    1. Install GNUstep sudo apt-get install gnustep gnustep-devel 2. Write hello world program, and save ...

  10. iOS 开发者能用上的 10 个 Xcode 插件

    本文由 伯乐在线 - 邢敏 翻译,黄利民 校稿.未经许可,禁止转载! 英文出处:code.tutsplus.com.欢迎加入翻译小组. 1. XcodeColors:给 Xcode 控制台添加颜色 2 ...