ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 1166. Design File System

2022-08-28 09:32:57  阅读:183  来源: 互联网

标签: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
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16632244.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有