Add and Search Word - Data structure design

leetcode第211题,归类标签是字典树,但是看到讨论区解法还是挺多的。这里最不好处理的就是“.”的处理,这里“.”可以代表任意字符,也就意味着遍历到有点符号的时候需要把所有的当前节点下的子节点全部遍历出来,一一比较,这里比较考验技巧了,其中我看到有一种采用递归方法,单独写一个递归函数,我参考了一种更容易理解的方法。


在这份代码中,首先注意count,这里是用来记录单词结束的,和上一个程序有所不同,这个标记可以记录有多少个单词在这个节点结束,其实大同小异,在这题目中没有太大作用,但是简化了代码量。

关键是这里采用了一个很关键的地方就是用一个列表记录子节点,当遇到‘.’符号的时候,子节点不再是一个,而是一个集合,所以把所有的子节点全部加入列表中记录,而后遍历这个列表中的节点,遍历到最后,这个集合里只要有单词结束标志,就意味着找到了。

class TrieNode(object):
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.count = 0
        self.childern = {}

class WordDictionary(object):

    def __init__(self):
        self.root = TrieNode()

    def addWord(self, word):
        node = self.root
        for c in word:
            if c in node.childern:
                node = node.childern[c]
            else:
                newNode = TrieNode()
                node.childern[c] = newNode
                node = newNode
        node.count += 1 

    def search(self, word):
        nodes = [self.root]
        for c in word:
            subNode = []
            for node in nodes:
                if c == '.':
                    subNode += node.childern.values()
                elif c in node.childern:
                    subNode.append(node.childern[c])
            nodes = subNode
        for node in nodes:
            if node.count > 0:
                return True
        return False
        

# Your WordDictionary object will be instantiated and called as such:
# wordDictionary = WordDictionary()
# wordDictionary.addWord("word")
# wordDictionary.search("pattern")



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Direct sequence spread spectrum (DSSS) is a modulation technique used in wireless communication systems to enhance the resistance to interference and increase the security of the communication. In this technique, the data signal is spread over a wider frequency band by multiplying it with a code sequence. The receiver then uses the same code sequence to despread the received signal and recover the original data. In order to achieve synchronization between the transmitter and receiver in a DSSS system, the receiver must synchronize its code sequence with the transmitter's code sequence. This synchronization can be achieved by using a synchronization code or by using a tracking loop. Matlab is a powerful tool for analyzing and simulating the performance of DSSS systems. It provides a wide range of functions and tools for implementing and testing DSSS systems. To analyze and simulate the synchronization performance of a DSSS system in Matlab, the following steps can be followed: 1. Design the DSSS system: This involves designing the spreading and despreading codes, the modulation scheme, and the receiver structure. 2. Generate the transmitted signal: Use Matlab functions to generate the transmitted signal by spreading the data signal with the spreading code. 3. Add noise: Add white Gaussian noise to the transmitted signal to simulate the effects of interference. 4. Implement the receiver: Implement the receiver structure in Matlab, including the synchronization code or tracking loop. 5. Perform synchronization: Use Matlab functions to perform synchronization between the transmitter and receiver. 6. Evaluate performance: Evaluate the performance of the DSSS system by calculating the bit error rate (BER) and signal-to-noise ratio (SNR) for different levels of interference and synchronization. 7. Optimize performance: Use Matlab tools to optimize the performance of the DSSS system by adjusting parameters such as the spreading code length, modulation scheme, and receiver structure. Overall, Matlab provides a powerful and flexible platform for analyzing and simulating the performance of DSSS systems, including the synchronization performance. By following the above steps and using Matlab's tools and functions, it is possible to design and optimize a robust and efficient DSSS system for wireless communication applications.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值