首页 > 其他分享 >LeetCode 1166. Design File System

LeetCode 1166. Design File System

时间:2022-08-28 09:11:06浏览次数:44  
标签:map 1166 return get createPath Design FileSystem path LeetCode

原题链接在这里:https://leetcode.com/problems/design-file-system/

题目:

You are asked to design a file system that allows you to create new paths and associate them with different values.

The format of a path is one or more concatenated strings of the form: / followed by one or more lowercase English letters. For example, "/leetcode" and "/leetcode/problems" are valid paths while an empty string "" and "/" are not.

Implement the FileSystem class:

  • bool createPath(string path, int value) Creates a new path and associates a value to it if possible and returns true. Returns false if the path already exists or its parent path doesn't exist.
  • int get(string path) Returns the value associated with path or returns -1 if the path doesn't exist.

Example 1:

Input: 
["FileSystem","createPath","get"]
[[],["/a",1],["/a"]]
Output: 
[null,true,1]
Explanation: 
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/a", 1); // return true
fileSystem.get("/a"); // return 1

Example 2:

Input: 
["FileSystem","createPath","createPath","get","createPath","get"]
[[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]]
Output: 
[null,true,true,2,false,-1]
Explanation: 
FileSystem fileSystem = new FileSystem();

fileSystem.createPath("/leet", 1); // return true
fileSystem.createPath("/leet/code", 2); // return true
fileSystem.get("/leet/code"); // return 2
fileSystem.createPath("/c/d", 1); // return false because the parent path "/c" doesn't exist.
fileSystem.get("/c"); // return -1 because this path doesn't exist. 

Constraints:

  • The number of calls to the two functions is less than or equal to 104 in total.
  • 2 <= path.length <= 100
  • 1 <= value <= 109

题解:

Main a map between path and value.

When createPath, get parent of path, if it doen't exist in the map, return false. 

Otherwise, map.putIfAbsent(path, value). if putIfAbsent doesn't return null, it means path already exists in the map, return false.

Get is to simply return the value from the map.

Time Complexity: craetePath, O(n). n = path.length. get, O(1).

Space: O(m*n). m = map.size(). n = path length.

AC Java:

 1 class FileSystem {
 2     Map<String, Integer> map;
 3     
 4     public FileSystem() {
 5         map = new HashMap<>();
 6         map.put("", -1);
 7     }
 8     
 9     public boolean createPath(String path, int value) {
10         int ind = path.lastIndexOf("/");
11         if(ind == -1){
12             return false;
13         }
14         
15         String parent = path.substring(0, ind);
16         if(!map.containsKey(parent)){
17             return false;
18         }
19         
20         return map.putIfAbsent(path, value) == null;
21     }
22     
23     public int get(String path) {
24         return map.getOrDefault(path, -1);
25     }
26 }
27 
28 /**
29  * Your FileSystem object will be instantiated and called as such:
30  * FileSystem obj = new FileSystem();
31  * boolean param_1 = obj.createPath(path,value);
32  * int param_2 = obj.get(path);
33  */

 

标签:map,1166,return,get,createPath,Design,FileSystem,path,LeetCode
From: https://www.cnblogs.com/Dylan-Java-NYC/p/16632244.html

相关文章

  • LeetCode/阶乘后的零
    1.返回尾零数量可以转换为求质因子为2和5数量的较小值,实际上就是求质因子为5的数量classSolution{public:inttrailingZeroes(intn){intans=0;......
  • 【重要】LeetCode 662. 二叉树最大宽度
    题目链接注意事项根据满二叉树的节点编号规则:若根节点编号为u,则其左子节点编号为u<<1,其右节点编号为u<<1|1。一个朴素的想法是:我们在DFS过程中使用两个哈希表......
  • leetcode 647. Palindromic Substrings回文子串(中等)
    一、题目大意给你一个字符串s,请你统计并返回这个字符串中回文子串的数目。回文字符串是正着读和倒过来读一样的字符串。子字符串是字符串中的由连续字符组成的一......
  • leetcode139:单词拆分
    packagecom.mxnet;importjava.util.HashSet;importjava.util.List;publicclassSolution139{publicstaticvoidmain(String[]args){}/**......
  • LeetCode刷题23-在排序数组中查找元素的第一个和最后一个位置
    importjava.util.Arrays;/***功能描述**@authorASUS*@version1.0*@Date2022/8/27*/publicclassMain06{publicstaticvoidmain(String[]......
  • leetcode169:多数元素
    packagecom.mxnet;importjava.util.HashMap;importjava.util.Set;publicclassSolution169{publicstaticvoidmain(String[]args){}/**......
  • [Oracle] LeetCode 348 Design Tic-Tac-Toe
    Assumethefollowingrulesareforthetic-tac-toegameonannxnboardbetweentwoplayers:Amoveisguaranteedtobevalidandisplacedonanemptybloc......
  • 动态规划——leetcode5、最长回文子串
    1、题目描述:2、解题方法:动态规划动态规划解题步骤:1、确定状态最后一步:如果s[i,...,j]是回文子串,那么需要满足两个条件①s[i......
  • LeetCode 20. 有效的括号
    题目题目链接:https://leetcode.cn/problems/valid-parentheses/给定一个只包括'(',')','{','}','[',']' 的字符串s,判断字符串是否有效。有效字符串需满足:左括号必须用相......
  • leetcode143-重排链表
    重排链表快慢指针+翻转链表通过快慢指针找到中间节点,然后将后半段进行翻转,然后与前半段进行拼接。classSolution{publicvoidreorderList(ListNodehead){......