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

思路:如果只是上面的要求,那么hashmap就可以解决;

class FileSystem {

    private HashMap<String, Integer> hashmap;
    public FileSystem() {
        this.hashmap = new HashMap<>();
        hashmap.put("", -1);
    }
    
    public boolean createPath(String path, int value) {
        int index = path.lastIndexOf("/");
        String parent = path.substring(0, index);
        if(!hashmap.containsKey(parent)) {
            return false;
        }
        return hashmap.putIfAbsent(path, value) == null;
    }
    
    public int get(String path) {
        return hashmap.getOrDefault(path, -1);
    }
}

/**
 * Your FileSystem object will be instantiated and called as such:
 * FileSystem obj = new FileSystem();
 * boolean param_1 = obj.createPath(path,value);
 * int param_2 = obj.get(path);
 */

但是,Doordash的onsite有delete的要求,如果path有children就不能delete;

  • delete(path) -> deletes a node, but ONLY if it has no children

如果用hashmap,那么delete就要loop所有的key,不是很efficient;那么如果用trie,只用search这个node以下的部分就可以了;所以这个题就是个变种的trie; 弄清楚了,其实也还好写;

class FileSystem {
    class TrieNode {
        public int value;
        public HashMap<String, TrieNode> children;
        public TrieNode() {
            this.value = -1;
            this.children = new HashMap<>();
        }
    }
    
    private TrieNode root;
    public FileSystem() {
        this.root = new TrieNode();
    }
    
    public boolean createPath(String path, int value) {
        TrieNode cur = root;
        String[] splits = path.split("/");
        int n = splits.length;
        // parent path; len - 1;
        // 注意是从1开始的,因为前面有个空格,不需要;
        for(int i = 1; i < n - 1; i++) {
            String word = splits[i];
            if(!cur.children.containsKey(word)) {
                return false;
            }
            cur = cur.children.get(word);
        }
        
        // last word;
        String lastword = splits[n - 1];
        if(cur.children.containsKey(lastword)) {
            return false;
        }
        cur.children.put(lastword, new TrieNode());
        TrieNode node = cur.children.get(lastword);
        node.value = value;
        return true;
    }
    
    public int get(String path) {
        TrieNode cur = root;
        String[] splits = path.split("/");
        // 注意是从1开始的,因为前面有个空格,不需要;
        for(int i = 1; i < splits.length; i++) {
            String word = splits[i];
            if(!cur.children.containsKey(word)) {
                return -1;
            }
            cur = cur.children.get(word);
        }
        return cur.value;
    }
    
    public boolean deletePath(String path) {
        TrieNode cur = root;
        String[] splits = path.split("/");
        // 注意是从1开始的,因为前面有个空格,不需要;
        for(int i = 1; i < splits.length - 1; i++) {
            String word = splits[i];
            if(!cur.children.containsKey(word)) {
                return false;
            }
            cur = cur.children.get(word);
        }
        
        //last word;
        String lastword = splits[splits.length - 1];
        TrieNode lastnode = cur.children.get(lastword);
        if(lastnode.children.size() == 0) {
            cur.children.remove(lastword);
            return true;
        } else {
            return false;
        }
    }
}

/**
 * Your FileSystem object will be instantiated and called as such:
 * FileSystem obj = new FileSystem();
 * boolean param_1 = obj.createPath(path,value);
 * int param_2 = obj.get(path);
 */

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值