当前位置:网站首页>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
边栏推荐
- ctfshow-web361(SSTI)
- 【科普】CRC校验(一)什么是CRC校验?
- Chondroitin sulfate in vitreous
- [mathematical modeling] - analytic hierarchy process (AHP)
- Use of content provider
- Go language GUI framework Fyne Chinese garbled or not displayed
- QT error: no matching member function for call to ‘connect‘
- CISSP certified daily knowledge points (April 19, 2022)
- Machine learning practice - naive Bayes
- Daily network security certification test questions (April 14, 2022)
猜你喜欢
os_ authent_ Prefix
Esp32 (UART 485 communication) - 485 communication of serial port (3)
ctfshow-web362(SSTI)
The first leg of the national tour of shengteng AI developer creation and enjoyment day was successfully held in Xi'an
机器学习实战 -朴素贝叶斯
【历史上的今天】4 月 23 日:YouTube 上传第一个视频;网易云音乐正式上线;数字音频播放器的发明者出生
On iptables
STM32: LCD显示
使用晨曦记账本,分析某个时间段每个账户收支结余
Halo open source project learning (VII): caching mechanism
随机推荐
微搭低代码零基础入门课(第三课)
STM32: LCD display
Daily network security certification test questions (April 13, 2022)
On iptables
ESP32 LVGL8. 1 - event (event 17)
Ucosiii transplantation and use, reference punctual atom
机器学习理论之(8):模型集成 Ensemble Learning
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
K210 serial communication
Esp32 (UART 485 communication) - 485 communication of serial port (3)
Simple use of navigation in jetpack
RPM package management
Deeply understand what new and make in golang are and what are the differences?
Esp32 (UART receiving and sending) - receiving and sending communication of serial port (4)
ESP32 LVGL8. 1 - img picture (IMG 20)
Daily CISSP certification common mistakes (April 14, 2022)
Promote QT default control to custom control
Machine learning theory (8): model integration ensemble learning
机器学习理论基础篇--关于机器学习的一些术语
Usage of functions decode() and replace() in SQL