zoukankan      html  css  js  c++  java
  • 676. 实现一个魔法字典




    class MagicDictionary(object):
        def __init__(self):
            """
            Initialize your data structure here.
            """
            self.mydict = {}
    
        def buildDict(self, dictionary):
            """
            :type dictionary: List[str]
            :rtype: None
            """
            for word in dictionary:
                self.mydict[word] = len(word)
    
        def search(self, searchWord):
            """
            :type searchWord: str
            :rtype: bool
            """
            # 特判:没有相同长度的单词,肯定为False
            if len(searchWord) not in self.mydict.values():
                return False
            # 长度相同,但单词不同,且不同字符只有一个,才能返回True,否则返回False
            for word, wordSize in self.mydict.items():
                if len(searchWord) == wordSize and searchWord != word and self.canTrans(searchWord, word, wordSize):
                    return True
            return False
    
        def canTrans(self, searchWord, word, wordSize):
            flag = 0
            for i in range(wordSize):
                if searchWord[i] != word[i]:
                    flag += 1
            return flag == 1
    
  • 相关阅读:
    课程总结第十一周
    用户场景分析
    团队冲刺10
    课程总结第十周
    团队冲刺09
    梦断代码阅读笔记03
    转发和重定向的区别
    request
    servletConfig
    servlet
  • 原文地址:https://www.cnblogs.com/panweiwei/p/14025051.html
Copyright © 2011-2022 走看看