Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Python solution for Leetcode. Moved tohttps://github.com/jw2013/Leetcode-Py

NotificationsYou must be signed in to change notification settings

leetcoders/Leetcode-Py

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This is my solution to Leetcode Online Judge's problems. Currently I am revamping the problems all over again towards more idiomatic Python. Stay tuned for updates.

Feel free to submit pull requests for submitting more elegant solution.

###List of idiomatic Python I prefer (as of now)

  • Prefer list comprehension over map / filter.
# from Gray Code# Baddef grayCode(self, n):        return map(lambda x: (x / 2) ^ x, range(1 << n))# Idiomaticdef grayCode(self, n):        return [(x / 2) ^ x for x in range(1 << n)]
  • Prefer usingin keyword over repetitive variable in conditional statement.
# from Post Order Traversal# Badif parent.right == None or parent.right == prev# Idiomaticif parent.right in (None, prev):
  • Prefer using docstring over single line comment when describing functionality of a method.
# from Search Insert Position# Bad# Iterative solution is also fine.def searchInsert(self, A, target):# Idiomaticdef searchInsert(self, A, target):    """Iterative solution is also fine.    """
  • Prefer implicit evaluation of condition (e.g.if,while, etc.) over explicit comparison in condition.
    Notice empty list and dictionary will be evaluated to False, so that is very handy.
# from Binary Tree Preorder Traversal# Badwhile len(stack) > 0:current = stack.pop()# Idiomaticwhile stack:current = stack.pop()
  • Preferis None over== None. Noticeis looks for referential equality, andNone is a singleton.
    The fundamental reason for this preference is much improved speed, and== can be overriden by__eq__.
# from Binary Tree Preorder Traversal# Badif root == None:# Idiomaticif root is None:

One interesting side note in Python regarding this is onStackoverflow. Trust me, that is worth your 60 seconds of time. But that only works in REPL though, not on a executable python file.

READ THIS or above two rules will only do you harm:
Sometimes you have to useif foo is not None overif foo. For example, if foo is 0, thenif foo will become False. But 0 is not None. Just watch out. A rule of the thumb is if you want to check if the default argument of a function is None, then useif foo is not None, otherwise you can most likely useif foo if you know what you are doing.

  • Consider using enumerate when index accessing looks verbose
# from Two Sum# Badfor i in range(len(nums)):        if target - nums[i] in lookup:            return (lookup[target - nums[i]] + 1, i + 1)        lookup[nums[i]] = i    # Idiomatic    for i, num in enumerate(nums):        if target - num in lookup:            return (lookup[target - num] + 1, i + 1)        lookup[num] = i
  • Readability counts.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." Martin Fowler is right.

# from Edit Distance# Baddef minDistance(self, word1, word2):        distance = [[i] for i in range(len(word1) + 1)]        distance[0] = [i for i in range(len(word2) + 1)]        for i in range(1, len(word1) + 1):            for j in range(1, len(word2) + 1):                distance[i].append(min(distance[i - 1][j] + 1, distance[i][j - 1] + 1, distance[i - 1][j - 1] + (word1[i - 1] != word2[j - 1])))        return distance[-1][-1]    # Idiomatic    def minDistance(self, word1, word2):        distance = [[i] for i in range(len(word1) + 1)]        distance[0] = [i for i in range(len(word2) + 1)]        for i in range(1, len(word1) + 1):            for j in range(1, len(word2) + 1):                deletion = distance[i - 1][j] + 1                addition = distance[i][j - 1] + 1                substitution = distance[i - 1][j - 1]                if word1[i - 1] != word2[j - 1]:                    substitution += 1                distance[i].append(min(deletion, addition, substitution))        return distance[-1][-1]

About

Python solution for Leetcode. Moved tohttps://github.com/jw2013/Leetcode-Py

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python100.0%

[8]ページ先頭

©2009-2025 Movatter.jp