当前位置:网站首页>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
边栏推荐
- Esp32 (UART 485 communication) - 485 communication of serial port (3)
- Daily CISSP certification common mistakes (April 15, 2022)
- 中金财富怎么样?在上边开户安全吗
- Sentinel规则持久化进Nacos
- Use of content provider
- ESP32 LVGL8. 1 - textarea text area (textarea 26)
- Usage of functions decode() and replace() in SQL
- QT excel operation summary
- K210串口通信
- CISSP certified daily knowledge points (April 14, 2022)
猜你喜欢
Esp32 (UART ecoh) - serial port echo worm learning (2)
MVVM模型
Use bitnami / PostgreSQL repmgr image to quickly set up PostgreSQL ha
12个例子夯实promise基础
One of the reasons why the WebView web page cannot be opened (and some WebView problem records encountered by myself)
根据快递单号查询物流查询更新量
STM32: LCD display
ESP32 LVGL8. 1 - roller rolling (roller 24)
Ctfshow - web362 (ssti)
os_authent_prefix
随机推荐
iptables初探
Chondroitin sulfate in vitreous
Screenshot using projectmediamanager
Excel intercept text
22 year flying Book manpower Kit
视频边框背景如何虚化,简单操作几步实现
kettle庖丁解牛第17篇之文本文件输出
Daily CISSP certification common mistakes (April 18, 2022)
CISSP certified daily knowledge points (April 15, 2022)
SQL中函数 decode()与 replace()的用法
机器学习理论基础篇--关于机器学习的一些术语
Implementation of TCP UDP communication with golang language
Esp32 (UART event) - serial port event learning (1)
iptables -L执行缓慢
Configure iptables
Esp32 (UART 485 communication) - 485 communication of serial port (3)
CANopen usage method and main parameters of object dictionary
Ionic instruction set order from creation to packaging
About the operation of unit file reading (I)
Redis common interview questions