Commit 93ba45cf by 牛家玺

批改

parent c6043871
# 方法一,笨办法,手动组合字典
def uniqueMorseRepresentations1(self, words):
ref = {'a':".-",
"b":"-...",
"c":"-.-.",
"d":"-..",
"e":".",
"f":"..-.",
"g":"--.",
"h":"....",
"i":"..",
"j":".---",
"k":"-.-",
"l":".-..",
"m":"--",
"n":"-.",
"o":"---",
"p":".--.",
"q":"--.-",
"r":".-.",
"s":"...",
"t":"-",
"u":"..-",
"v":"...-",
"w":".--",
"x":"-..-",
"y":"-.--",
"z":"--.."}
ref = {'a': ".-",
"b": "-...",
"c": "-.-.",
"d": "-..",
"e": ".",
"f": "..-.",
"g": "--.",
"h": "....",
"i": "..",
"j": ".---",
"k": "-.-",
"l": ".-..",
"m": "--",
"n": "-.",
"o": "---",
"p": ".--.",
"q": "--.-",
"r": ".-.",
"s": "...",
"t": "-",
"u": "..-",
"v": "...-",
"w": ".--",
"x": "-..-",
"y": "-.--",
"z": "--.."}
res = []
for word in words:
s = ''
......@@ -36,21 +34,23 @@ def uniqueMorseRepresentations1(self, words):
res.append(s)
print(len(set(res)))
uniqueMorseRepresentations1(self=uniqueMorseRepresentations1,words=["gin", "zen", "gig", "msg"])
uniqueMorseRepresentations1(self=uniqueMorseRepresentations1, words=["gin", "zen", "gig", "msg"])
# 利用字典的zip函数拼接字母和摩尔斯密码为字典
def uniqueMorseRepresentations2(self, words):
morsecode = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
charlist = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
morsemap = dict(zip(charlist,morsecode))
morselist=[]
morsecode = [".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.",
"---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."]
charlist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
"v", "w", "x", "y", "z"]
morsemap = dict(zip(charlist, morsecode))
morselist = []
for i in words:
s = ""
for j in i:
s += morsemap.get(j)
#print(s)
# print(s)
morselist.append(s)
......@@ -58,7 +58,7 @@ def uniqueMorseRepresentations2(self, words):
print(len(set(morselist)))
uniqueMorseRepresentations2(self=uniqueMorseRepresentations2,words=["gin", "zen", "gig", "msg"])
uniqueMorseRepresentations2(self=uniqueMorseRepresentations2, words=["gin", "zen", "gig", "msg"])
# 方法三用ord()函数求出a的ASCII值, chr() 是将ASCII值变为字符. ord('a') = 97 , ord("A") = 65
......@@ -69,4 +69,6 @@ def uniqueMorseRepresentations3(self, words):
print(lenth)
uniqueMorseRepresentations3(self=uniqueMorseRepresentations3,words=["gin", "zen", "gig", "msg"])
\ No newline at end of file
uniqueMorseRepresentations3(self=uniqueMorseRepresentations3, words=["gin", "zen", "gig", "msg"])
# 实现的算法没有问题 ,三种方法可以看出逐步优化,如果是在项目中可以把 morsecode 提取到方法之外保证只初始化一次
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment