当前位置:网站首页>Simplified path (force buckle 71)
Simplified path (force buckle 71)
2022-04-23 18:48:00 【Xiao Qin doesn't lose his hair】
Deque,Queue,stack Simple understanding
Deque Implement a two terminal queue ,Queue fifo
| Queue | Deque | |
|---|---|---|
| Add elements to the end of the team | add()/offer() | addLast()/offerLast() |
| Take the first element of the team and delete | remove()/poll() | removeFirst()/pollFirst() |
| Take the first element of the team but do not delete | element()/peek() | getFirst()/peekFirst() |

stack( Stack , The principle is First in, then out , Last in, first out ) Common methods :
①push() Push
②top() Get the stack top element
③pop() Pop up top element
④empty() It can detect stack If the inside is empty , return true It's empty , return false Is not empty
⑥size() return stack The number of internal elements
Simplified path
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 .
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String path = sc.next();
simplifyPath(path);
}
public static String simplifyPath(String path) {
String[] str = path.split("/");// according to "/" Split paths into arrays
Deque<String> deque = new ArrayDeque<String>();
for (String string : str) {
if (string.equals("..")) {// ".." Switch to the previous Directory , Therefore, you need to delete a layer of path
if (!string.isEmpty()) {
deque.pollLast(); // Here is to delete a layer of path
}
} else if (string.length() > 0 && !".".equals(string)) {// That is, the current element length of the array is greater than 0, And not for "."
deque.offerLast(string);// Add the current element to ArrayDeque A party
}
}
StringBuffer sbf = new StringBuffer();
if (deque.isEmpty()) {
sbf.append("/");
} else {
while (!deque.isEmpty()) {
sbf.append("/");
sbf.append(deque.pollFirst());
}
}
System.out.println(sbf.toString());
return sbf.toString();
}
}
Shallow deque.isEmpty() Changed to deque==null, If you change it all to something like this, you'll Out of memory ,so Or more isEmpty() Well !
Make complaints : Great people look at the problem , The cook looked at the solution , I have to search the solution of this kind of dishes Deque How to use !!! Type it first and understand , Then type again after understanding

版权声明
本文为[Xiao Qin doesn't lose his hair]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231847462774.html
边栏推荐
猜你喜欢

Simple use of navigation in jetpack

从技术体系到商业洞察,中小研发团队架构实践之收尾篇

Download xshell 6 and xftp6 official websites

使用 bitnami/postgresql-repmgr 镜像快速设置 PostgreSQL HA

玻璃体中的硫酸软骨素

七、DOM(下) - 章节课后练习题及答案

Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha

iptables -L执行缓慢

Esp32 (UART event) - serial port event learning (1)

ESP32 LVGL8. 1 - textarea text area (textarea 26)
随机推荐
微搭低代码零基础入门课(第三课)
The corresponding permissions required to automatically open the app in the setting interface through accessibility service
配置iptables
listener. log
Esp32 (UART ecoh) - serial port echo worm learning (2)
解决:cnpm : 无法加载文件 ...\cnpm.ps1,因为在此系统上禁止运行脚本
SQL中函数 decode()与 replace()的用法
[mathematical modeling] - analytic hierarchy process (AHP)
Sentinel rule persistence into Nacos
Query the logistics update quantity according to the express order number
QT excel operation summary
Setting up keil environment of GD single chip microcomputer
STM32: LCD display
Go language GUI framework Fyne Chinese garbled or not displayed
数据库上机实验四(数据完整性与存储过程)
Nacos cluster construction and MySQL persistence configuration
ESP32 LVGL8. 1 - msgbox message box (msgbox 28)
Daily network security certification test questions (April 13, 2022)
Advanced transfer learning
Nacos as service registry
