力扣第820题 单词的压缩编码

力扣第820题 单词的压缩编码

class TrieNode
    {
public:
	map<char, TrieNode*> children;
};
class Solution {
public:
    void GetNum(TrieNode * node, int num, int& count)
    {
        if (node == NULL || node->children.size() == 0)
        {
            count += num + 1;
            return;
        }
        for (map<char, TrieNode*>::iterator itor = node->children.begin(); itor != node->children.end(); itor++)
        {
            GetNum(itor->second, 1 + num, count );
        }
    }

    int minimumLengthEncoding(vector<string>& words)
    {
        TrieNode* root = new TrieNode();
        TrieNode* temp;
        for (string s : words)
        {
            temp = root;
            for (int i = s.size() - 1; i >= 0; i--)
            {
                if (temp->children[s[i]] == NULL)
                {
                    temp->children[s[i]] = new TrieNode();
                }
                temp = temp->children[s[i]];
            }
        }
        int count = 0;
        GetNum(root, 0, count);
        return count;
    }

};

posted on 2020-03-31 19:52  woodjay  阅读(135)  评论(0编辑  收藏  举报

导航