leetcode 1166. 设计文件系统(C++)

你需要设计一个能提供下面两个函数的文件系统:

  • create(path, value): 创建一个新的路径,并尽可能将值 value 与路径 path 关联,然后返回 True。如果路径已经存在或者路径的父路径不存在,则返回 False
  • get(path): 返回与路径关联的值。如果路径不存在,则返回 -1

“路径” 是由一个或多个符合下述格式的字符串连接起来形成的:在 / 后跟着一个或多个小写英文字母。

例如 /leetcode 和 /leetcode/problems 都是有效的路径,但空字符串和 / 不是有效的路径。

好了,接下来就请你来实现这两个函数吧!(请参考示例以获得更多信息)

 

示例 1:

输入: 
["FileSystem","create","get"]
[[],["/a",1],["/a"]]
输出: 
[null,true,1]
解释: 
FileSystem fileSystem = new FileSystem();

fileSystem.create("/a", 1); // 返回 true
fileSystem.get("/a"); // 返回 1

示例 2:

输入: 
["FileSystem","create","create","get","create","get"]
[[],["/leet",1],["/leet/code",2],["/leet/code"],["/c/d",1],["/c"]]
输出: 
[null,true,true,2,false,-1]
解释:
FileSystem fileSystem = new FileSystem();

fileSystem.create("/leet", 1); // 返回 true
fileSystem.create("/leet/code", 2); // 返回 true
fileSystem.get("/leet/code"); // 返回 2
fileSystem.create("/c/d", 1); // 返回 false 因为父路径 "/c" 不存在。
fileSystem.get("/c"); // 返回 -1 因为该路径不存在。

 

提示:

  • 对两个函数的调用次数加起来小于等于 10^4
  • 2 <= path.length <= 100
  • 1 <= value <= 10^9

C++

class FileSystem {
public:
    map<string,int> tmp;
    FileSystem() 
    {
        
    }
    
    bool create(string path, int value) 
    {
        if(""==path || "/"==path)
        {
            return false;
        }
        if(tmp[path])
        {
            return false;
        }
        int n=path.length();
        int start=0;
        for(int i=n-1;i>=0;i--)
        {
            if('/'==path[i])
            {
                start=i;
                break;
            }
        }
        if(0==start)
        {
            tmp[path]=value;
            return true;            
        }
        string ss=path.substr(0,start);
        if(0==tmp[ss])
        {
            return false;
        }
        else
        {
            tmp[path]=value;
            return true;
        }     
    }
    
    int get(string path) 
    {
        if(tmp[path])
        {
            return tmp[path];
        }
        else
        {
            return -1;
        }
    }
};

/**
 * Your FileSystem object will be instantiated and called as such:
 * FileSystem* obj = new FileSystem();
 * bool param_1 = obj->create(path,value);
 * int param_2 = obj->get(path);
 */

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值