当前位置:网站首页>JS implementation force deduction 71 question simplified path
JS implementation force deduction 71 question simplified path
2022-07-06 20:09:00 【Will you give me good night】
Long time no see, after about ... Forget hahaha , Doing in recently MongoDB, It feels very suitable for the front end hahaha , Don't talk much , What I bring to you today is , An algorithm problem solution , Maybe it's too simple , For some big men inside , Cough , bosses , Just have fun , Don't talk nonsense , What I'm going to tell you is the number one of Li Kou 71 topic , The problem of simplified path is a medium problem , Take a challenge , Now let's look at the question .
Give you a string path , Indicates a point to a file or directory Unix style Absolute path ( With '/' start ), Please translate it into a more concise specification path .
stay Unix Style file system , One point (.) Represents the current directory itself ; Besides , Two points (..) Means to switch the directory to the next level ( Point to the parent directory ); Both can be part of a complex relative path . Any number of consecutive slashes ( namely ,'//') Are treated as a single slash '/' . For this problem , Points in any other format ( for example ,'...') Are considered documents / Directory name .
Please note that , Back to Canonical path The following format must be followed :
Always with slash '/' start .
There must be only one slash between two directory names '/' .
Last directory name ( If there is ) You can't With '/' ending .
Besides , The path contains only directories on the path from the root directory to the target file or directory ( namely , Not included '.' or '..').
Return the simplified Canonical path .
Example 1:
Input :path = "/home/"
Output :"/home"
You see the question , Xiaobai may have been confused , What is this big paragraph , Let's not rush , We begin to explain our ideas .
- Let's first think about the common characters in the question , Is it right? ‘/’ Right, now that we have found it ‘/’, The first function we think of is split Right , This can put the string '/' Remove and become an array , Now that it has become an array , Should we think of a data structure called stack again , although js There is no stack data structure in , But we can simulate a .
- Now let's analyze the requirements : First of all :'.' Represents the current directory itself : In other words /a/./b === /a/b It can be removed ; second :'..' Represents the previous directory of the current directory : In other words /a/b/../c === /a/c It can also be removed , And also remove the directory in front of him ;
After analyzing this problem, let's implement it with code :
/**
* @param {string} path
* @return {string}
*/
The unimportant ones above are some notes : Explain it.
/**
* The first representation has a parameter path The type is character
* The second represents that the returned parameter is also of character type
*/
The above explanation looks friendly to some novices. Let's start with the code
const simplifyPath = function (path) {
let stack = []; Define an empty array
let str = ''; Define a string
Why are the above two so defined? I won't talk about it first to avoid too much confusion
let arr = path.split('/'); Press '/' Split what we mentioned above , This becomes an array, which looks like this ['','a','','.','b','','..','','..','','..','','c',''];
Now let's start to facilitate this array , Here I use forEach The performance may be poor for The cycle will be better .
arr.forEach(item => {
The judgment here is that if a term in the array is equal to '..' We will pop get out , If not equal to '.' stay push Enter the array we defined stack, You may ask why it doesn't mean '.' Well , Here's an explanation , because '.' Represents the current directory , So rule this out '.'
if (item && item === '..') {
stack.pop();
} else if (item && item !== '.') {
stack.push(item);
}
})
Here's to judge arr If the length is empty, it will return directly '/', If it is not empty, return the array of our loop ;
arr.length ? str = '/' + stack.join('/') : str = '/'
return str;
};
console.log(simplifyPath("/a/./b/../../c/"))
Through the above code, we can achieve the force buckle 71 topic , Ha ha ha , Is it simple , I wish you all more and more on the road of your algorithm , Enter the company you want to enter , Thank you. .
边栏推荐
- BUUCTF---Reverse---easyre
- Tencent T2 Daniel explained in person and doubled his job hopping salary
- js获取浏览器系统语言
- 《数字经济全景白皮书》保险数字化篇 重磅发布
- Example of shutter text component
- What happened to the kernel after malloc() was transferred? Attached malloc () and free () implementation source
- 【GET-4】
- 句号压缩过滤器
- 技术分享 | 抓包分析 TCP 协议
- 5. 無線體內納米網:十大“可行嗎?”問題
猜你喜欢
New generation garbage collector ZGC
01 基础入门-概念名词
[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
BUUCTF---Reverse---easyre
[play with Linux] [docker] MySQL installation and configuration
Configuration and simple usage of the EXE backdoor generation tool quasar
[network planning] Chapter 3 data link layer (4) LAN, Ethernet, WLAN, VLAN
腾讯架构师首发,2022Android面试笔试总结
A5000 vGPU显示模式切换
语音识别(ASR)论文优选:全球最大的中英混合开源数据TALCS: An Open-Source Mandarin-English Code-Switching Corpus and a Speech
随机推荐
8086指令码汇总表(表格)
腾讯架构师首发,2022Android面试笔试总结
Alibaba data source Druid visual monitoring configuration
Web开发小妙招:巧用ThreadLocal规避层层传值
It's enough to read this article to analyze the principle in depth
HMS Core 机器学习服务打造同传翻译新“声”态,AI让国际交流更顺畅
新一代垃圾回收器—ZGC
Social recruitment interview experience, 2022 latest Android high-frequency selected interview questions sharing
BUUCTF---Reverse---easyre
The "white paper on the panorama of the digital economy" has been released with great emphasis on the digitalization of insurance
[infrastructure] deployment and configuration of Flink / Flink CDC (MySQL / es)
【Yann LeCun点赞B站UP主使用Minecraft制作的红石神经网络】
【GET-4】
Example of applying fonts to flutter
Standardized QCI characteristics
案例 ①|主机安全建设:3个层级,11大能力的最佳实践
Notes on beagleboneblack
腾讯字节等大厂面试真题汇总,网易架构师深入讲解Android开发
Le lancement du jupyter ne répond pas après l'installation d'Anaconda
An East SMS login resurrection installation and deployment tutorial