題目
解題思路
簡單說一下這道題,難點在:讀不懂題目!!!
其實這道題說的是一個單詞在字典裏面的出現順序問題。
用中文詞彙舉例子:
蘋果(pingguo),香蕉(xiangjiao)
因爲我們的字母默認順序爲:
abcdefghijklmnopqrstuvwxyz
所以,在字典裏 蘋果(pingguo)會出現在 香蕉(xiangjiao)的前面。
但是,外星人的字母默認順序可能不同,這個題目就是要我們判斷 words 裏面單詞的順序是否滿足外星人字典的排列順序。
參考代碼
'''
@auther: Jedi.L
@Date: Thu, May 30, 2019 6:02
@Email: [email protected]
@Blog: www.tundrazone.com
'''
class Solution:
def isAlienSorted(self, words, order):
alien_dic = {}
# create alien dictionary by using enumerate
for i, c in enumerate(order):
alien_dic[c] = i
dic_order = sorted(words, key=lambda x: [alien_dic[c] for c in x])
return dic_order == words
如何刷題 : Leetcode 題目的正確打開方式
我的GitHub : GitHub
其他題目答案:leetcode題目答案講解彙總(Python版 持續更新)
其他好東西: MyBlog----苔原帶