🇨🇳
阿臻的学习笔记
  • 🤖AI
    • 📑README
    • 🕒Scheduling
      • 一种JSSP的DRL环境
    • 📜Paper
      • 神经协同过滤
      • 非侵入信号深度学习
      • 肾透析移植机器学习
      • 心理学随机森林
      • P300数据学习
    • ⚒️Pytorch
      • 1.1数据基础
      • 1.2自动梯度
      • 1.3神经网络
      • 1.4模型实现
      • 2数据操作
    • 🛠️Ray+Gym
    • 📃Graph Neural
      • 图神经网络基础
      • Contrastive Multi-View Representation Learning on Graphs
    • 📽️Deep Learning
      • 《第一章》
      • 《第二章》
      • 《第三章》
      • 《第四章》
      • 台湾陈蕴侬视频2020
    • 🔨MXNet
      • 《第一章》《第二章》
      • 《第三章》
      • 《第四章》
      • 《第五章》
      • 《第六章》
      • 《第七章》
      • 《第八章》
      • 《第九章》
      • 《第十章》
  • 👨‍🏭Study
    • 📔Algorithm
      • Leetcode
        • 第一天(乱刷)
        • 第二天(栈)
        • 第三天(栈)
        • 第四天(堆)(贪心)
        • 第五天(贪心)
        • 第六天(贪心)
        • 第七天(排序)
        • 第八天(排序)
        • 第九天(排序)
        • 第十天(位运算)
        • 第十一天(位运算)
        • 第十二天(位运算)
        • 第十三天(位运算)
        • 第十四天(树)
        • 第十五天(树)
        • 第十六天(树)
        • 第十七天(树)
        • 第十八天(树)
        • 第十九天(树)
        • 第二十天(树)
        • 第二十一天(树)
        • 第二十二天(并查集)
        • 第二十三天(并查集)
        • 第二十四天(DFS)(图)
        • 第二十五天(图)(设计)
        • 第二十六天(拓扑)
        • 第二十七天(字典树)
        • 第二十八天(字典树)
        • 第二十九天(树状数组)(二叉搜索树)
        • 第三十天(递归)
        • 第三十一天(脑筋急转弯)
        • 第三十二天(脑筋急转弯)
        • 第三十三天(记忆化)
        • 第三十四天(队列)
        • 第三十五天(队列)
        • 第三十六天(极小化极大)
        • 第三十七天(几何)
        • 第三十八天(蓄水池抽样)
        • 第三十九天(数组)
        • 第四十天(数组)
        • 第四十一天(数组)
        • 第四十二天(数组)
        • 第四十三天(数组)
        • 第四十四天(数组)
        • 第四十五天(数组)
        • 第四十六天(数组)
      • Sort
        • 最小堆
        • 归并排序(merge_sort)
    • 📓Knowledge
      • python补码
    • 🔧Other
      • pythonic语法
      • Ubuntu备忘
由 GitBook 提供支持
在本页

这有帮助吗?

导出为 PDF
  1. Study
  2. Algorithm
  3. Leetcode

第二十八天(字典树)

今日完成题目:208,677,421 208:实现前缀树

class TrieNode:
    def __init__(self):
        self.end=False # 表示是否存在某个结点
        self.children=collections.defaultdict(TrieNode) # 儿子字典,所有键默认为TrieNode

class Trie:

    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.root = TrieNode()


    def insert(self, word: str) -> None:
        """
        Inserts a word into the trie.
        """
        node = self.root
        for s in word:
            node = node.children[s] # node.children[s]默认为TrieNode,这里自动新建儿子
        node.end = True
        # print("insert " + word)


    def search(self, word: str) -> bool:
        """
        Returns if the word is in the trie.
        """
        node = self.root
        # print("search " + word)
        for s in word:
            node = node.children.get(s) # 获得键s,没有则返回None
            if node is None: # 前缀匹配失败
                return False
        if node.end: # 该字符有结尾,即匹配成功
            return True
        else:
            return False

    def startsWith(self, prefix: str) -> bool:
        """
        Returns if there is any word in the trie that starts with the given prefix.
        """
        node = self.root
        for s in prefix:
            node = node.children.get(s) # 获得键s,没有则返回None
            if node is None: # 前缀匹配失败
                return False
        return True

677:键值映射

  • 在实现前缀树的方法中,修改END为val,代表有无值,默认为0

  • 计算总和时候,历匹配前缀后,bfs遍所有儿子结点的值

421:数组中两个数的最大异或值

  • 首先计算数组中最大数的二进制长度 L。

  • 初始化 max_xor = 0。

  • 从 i = L - 1遍历到 i = 0(代表着从最左侧的比特位 L - 1遍历到最右侧的比特位 00):

    • 将 max_xor 左移,释放出下一比特位的位置。

    • 初始化 curr_xor = max_xor | 1(即将 max_xor 最右侧的比特置为 1)。

    • 遍历 nums,计算出长度为 L - i 的所有可能的按位前缀。

      • 将长度为 L - i 的按位前缀加入哈希集合 prefixes,按位前缀的计算公式如下:num >> i。

    • 遍历所有可能的按位前缀,检查是否存在 p1,p2 使得 p1^p2 == curr_xor。比较简单的做法是检查每个 p,看 curr_xor^p 是否存在。

      • 如果存在,就将 max_xor 改为 curr_xor(即将 max_xor 最右侧的比特位改为 1)。

      • 如果不存在,max_xor 最右侧的比特位继续保持为 0。

  • 返回 max_xor。

class Solution:
    def findMaximumXOR(self, nums: List[int]) -> int:
        L = len(bin(max(nums))) - 2 # 最大数值的二进制长度
        max_xor = 0
        for i in range(L)[::-1]: # 从最大位开始遍历
            # go to the next bit by the left shift
            max_xor <<= 1 
            # curr_xor的最后一位默认为1
            curr_xor = max_xor | 1
            # compute all existing prefixes 
            # of length (L - i) in binary representation
            prefixes = {num >> i for num in nums} # 所有前缀
            # Update max_xor, if two of these prefixes could result in curr_xor.
            # Check if p1^p2 == curr_xor, i.e. p1 == curr_xor^p2
            # print(max_xor,curr_xor, prefixes)
            max_xor |= any(curr_xor^p in prefixes for p in prefixes)
                    
        return max_xor
上一页第二十七天(字典树)下一页第二十九天(树状数组)(二叉搜索树)

最后更新于3年前

这有帮助吗?

👨‍🏭
📔