当前位置:网站首页>js实现力扣71题简化路径
js实现力扣71题简化路径
2022-07-06 11:42:00 【会给我晚安吗】
很久不见时隔大概...忘记了哈哈哈哈,最近在搞MongoDB,感觉很适合前端哈哈哈,话不多说,今天给大家带来的是,一道算法题题解,可能太简单了,对于内些大佬来说,咳咳,大佬,就看乐子就好啦,不废话了,给大家讲的是力扣的第71题,简化路径这道题呢是一个中等题,挑战一下吧,下面我们来看题。
给你一个字符串 path ,表示指向某一文件或目录的 Unix 风格 绝对路径 (以 '/' 开头),请你将其转化为更加简洁的规范路径。
在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。任意多个连续的斜杠(即,'//')都被视为单个斜杠 '/' 。 对于此问题,任何其他格式的点(例如,'...')均被视为文件/目录名称。
请注意,返回的 规范路径 必须遵循下述格式:
始终以斜杠 '/' 开头。
两个目录名之间必须只有一个斜杠 '/' 。
最后一个目录名(如果存在)不能 以 '/' 结尾。
此外,路径仅包含从根目录到目标文件或目录的路径上的目录(即,不含 '.' 或 '..')。
返回简化后得到的 规范路径 。
示例 1:
输入:path = "/home/"
输出:"/home"
大家看到题,小白可能已经懵了,这是啥一大段文字,大家先不要急,我们开始讲解思路。
- 我们先思考题中有共同的字符是啥,是不是‘/’对吧我们既然找到了‘/’,那我们首先想到的函数就是split对吧,这个可以把字符串的'/'去掉变成数组,既然变成了数组,我们是不是就又应该想到一个叫做栈的数据结构对吧,虽然js中没有栈这个数据结构,但是我们可以通过算法模拟一个。
- 下面我们分析需求:第一:'.' 代表的是当前目录本身: 换句话说就是 /a/./b === /a/b 它是可以去掉的;第二:'..'代表的是当前目录的前一个目录: 换句话说就是 /a/b/../c === /a/c 它也是可以去掉的,而且还要把他前面的目录去掉;
分析完了这道题下面我们来用代码去实现一下:
/**
* @param {string} path
* @return {string}
*/
上面那些不重要是一些注释:解释一下
/**
* 第一个代表有一个参数是path类型是字符型
* 第二个代表返回的参数也是字符型
*/
上面解释给一些新手看起来友好一些下面我们开始讲代码
const simplifyPath = function (path) {
let stack = []; 定义一个空数组
let str = ''; 定义一个字符串
上面这俩为啥这么定义我先不讲避免太混乱
let arr = path.split('/'); 把传进来的字符串按'/'分割我们上面讲到的,这里变成了一个数组大概是这个样子['','a','','.','b','','..','','..','','..','','c',''];
下面我们开始便利这个数组,这里我用到的forEach性能可能会差点for循环会更好一点。
arr.forEach(item => {
这里这个判断就是如果数组里面的一项等于'..'我们就pop出去,如果不等于'.'在push进我们定义的数组stack,可能大家会这样问为啥要不等于'.'呢,这里给大家解释一下,因为'.'代表当前的目录,所以要排除这个'.'
if (item && item === '..') {
stack.pop();
} else if (item && item !== '.') {
stack.push(item);
}
})
这里判断一下arr的长度如果长度为空的话就直接返回'/',不为空再返回我们循环的数组;
arr.length ? str = '/' + stack.join('/') : str = '/'
return str;
};
console.log(simplifyPath("/a/./b/../../c/"))通过上面代码就实现了力扣的71题,哈哈哈,是不是很简单,祝大家在自己的算法的道路上越来越来,进入到自己想进的公司,谢谢大家。
边栏推荐
- [玩转Linux] [Docker] MySQL安装和配置
- Understand yolov1 Part II non maximum suppression (NMS) in prediction stage
- It's super detailed in history. It's too late for you to read this information if you want to find a job
- Swagger2 reports an error illegal DefaultValue null for parameter type integer
- LeetCode_ Gray code_ Medium_ 89. Gray code
- Interview assault 63: how to remove duplication in MySQL?
- 凤凰架构2——访问远程服务
- Learn to explore - use pseudo elements to clear the high collapse caused by floating elements
- Excel 中VBA脚本的简单应用
- Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
猜你喜欢

Mind map + source code + Notes + project, ByteDance + JD +360+ Netease interview question sorting

zabbix 代理服务器 与 zabbix-snmp 监控

Mysql Information Schema 學習(一)--通用錶

How to customize animation avatars? These six free online cartoon avatar generators are exciting at a glance!

A method of removing text blur based on pixel repair
![[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer](/img/8c/afb90128e7a523bbee4c6c4166363f.png)
[calculating emotion and thought] floor sweeper, typist, information panic and Oppenheimer
受益匪浅,安卓面试问题

Computer network: sorting out common network interview questions (I)

【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...

Understand yolov1 Part II non maximum suppression (NMS) in prediction stage
随机推荐
Low CPU load and high loadavg processing method
Systematic and detailed explanation of redis operation hash type data (with source code analysis and test results)
Analysis of rainwater connection
【pytorch】yolov5 训练自己的数据集
ZABBIX proxy server and ZABBIX SNMP monitoring
Mysql Information Schema 學習(一)--通用錶
保证接口数据安全的10种方案
Black Horse - - Redis Chapter
[玩转Linux] [Docker] MySQL安装和配置
Mysql Information Schema 学习(二)--Innodb表
Interpretation of Dagan paper
Hudi vs Delta vs Iceberg
Detailed idea and code implementation of infix expression to suffix expression
JDBC details
腾讯Android面试必问,10年Android开发经验
接雨水问题解析
《数字经济全景白皮书》保险数字化篇 重磅发布
C # use Marshall to manually create unmanaged memory in the heap and use
[translation] micro survey of cloud native observation ability. Prometheus leads the trend, but there are still obstacles to understanding the health of the system
[translation] supply chain security project in toto moved to CNCF incubator