题解 | #字典树的实现#

字典树的实现

http://www.nowcoder.com/practice/a55a584bc0ca4a83a272680174be113b

class Solution {
public:
    /**
     * 
     * @param operators string字符串vector<vector<>> the ops
     * @return string字符串vector
     */
    unordered_map<string, int> mp,pre;
    vector<string> trieU(vector<vector<string> >& operators) {
        // write code here
        vector<string> ans;
        for(int i = 0;i < operators.size();i++){
           string op = operators[i][0],str = operators[i][1];
            if(op == "1"){
                mp[str]++;
                for(int i = 1;i <= str.size();i++){
                    pre[str.substr(0,i)]++;
                }
            }
            if(op == "2"){
                mp[str]--;
                for(int i = 1;i <= str.size();i++){
                    pre[str.substr(0,i)]--;
                }
            }
            if(op == "3"){
                if(mp[str])    ans.push_back("YES");
                else     ans.push_back("NO");
            }
            if(op == "4"){
                ans.push_back(to_string(pre[str]));
            }
        }
        return ans;
    }
};
全部评论

相关推荐

不亏是提前批,神仙打架,鼠鼠不配了
站队站对牛:现在92都报工艺岗了
投递韶音科技等公司7个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务