【Leetcode】386. Lexicographical Numbers

想到的是變成字符然後排序。
version 1 :

def lexicalOrder(self, n):
     """
     :type n: int
     :rtype: List[int]
     """
     res=[]
     for i in range(n+1):
         res.append(str(i))
     return map(lambda x: int(x),sorted(res)[1:])

version 2:
改進用到了sorted函數裏的key

def lexicalOrder(self, n):
    return sorted(range(1, n+1), key=str)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章