Commit 93ba45cf by 牛家玺

批改

parent c6043871
# 方法一,笨办法,手动组合字典 # 方法一,笨办法,手动组合字典
def uniqueMorseRepresentations1(self, words): def uniqueMorseRepresentations1(self, words):
ref = {'a':".-", ref = {'a': ".-",
"b":"-...", "b": "-...",
"c":"-.-.", "c": "-.-.",
"d":"-..", "d": "-..",
"e":".", "e": ".",
"f":"..-.", "f": "..-.",
"g":"--.", "g": "--.",
"h":"....", "h": "....",
"i":"..", "i": "..",
"j":".---", "j": ".---",
"k":"-.-", "k": "-.-",
"l":".-..", "l": ".-..",
"m":"--", "m": "--",
"n":"-.", "n": "-.",
"o":"---", "o": "---",
"p":".--.", "p": ".--.",
"q":"--.-", "q": "--.-",
"r":".-.", "r": ".-.",
"s":"...", "s": "...",
"t":"-", "t": "-",
"u":"..-", "u": "..-",
"v":"...-", "v": "...-",
"w":".--", "w": ".--",
"x":"-..-", "x": "-..-",
"y":"-.--", "y": "-.--",
"z":"--.."} "z": "--.."}
res = [] res = []
for word in words: for word in words:
s = '' s = ''
...@@ -36,21 +34,23 @@ def uniqueMorseRepresentations1(self, words): ...@@ -36,21 +34,23 @@ def uniqueMorseRepresentations1(self, words):
res.append(s) res.append(s)
print(len(set(res))) print(len(set(res)))
uniqueMorseRepresentations1(self=uniqueMorseRepresentations1,words=["gin", "zen", "gig", "msg"])
uniqueMorseRepresentations1(self=uniqueMorseRepresentations1, words=["gin", "zen", "gig", "msg"])
# 利用字典的zip函数拼接字母和摩尔斯密码为字典 # 利用字典的zip函数拼接字母和摩尔斯密码为字典
def uniqueMorseRepresentations2(self, words): def uniqueMorseRepresentations2(self, words):
morsecode = [".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."] 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)) charlist = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
morselist=[] "v", "w", "x", "y", "z"]
morsemap = dict(zip(charlist, morsecode))
morselist = []
for i in words: for i in words:
s = "" s = ""
for j in i: for j in i:
s += morsemap.get(j) s += morsemap.get(j)
#print(s) # print(s)
morselist.append(s) morselist.append(s)
...@@ -58,7 +58,7 @@ def uniqueMorseRepresentations2(self, words): ...@@ -58,7 +58,7 @@ def uniqueMorseRepresentations2(self, words):
print(len(set(morselist))) 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 # 方法三用ord()函数求出a的ASCII值, chr() 是将ASCII值变为字符. ord('a') = 97 , ord("A") = 65
...@@ -69,4 +69,6 @@ def uniqueMorseRepresentations3(self, words): ...@@ -69,4 +69,6 @@ def uniqueMorseRepresentations3(self, words):
print(lenth) print(lenth)
uniqueMorseRepresentations3(self=uniqueMorseRepresentations3,words=["gin", "zen", "gig", "msg"]) uniqueMorseRepresentations3(self=uniqueMorseRepresentations3, words=["gin", "zen", "gig", "msg"])
\ No newline at end of file
# 实现的算法没有问题 ,三种方法可以看出逐步优化,如果是在项目中可以把 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