Other articles

  1. LeetCode: 401. Binary Watch

    The problem is https://leetcode.com/problems/binary-watch/. Because the input size is limited, generating every output is workable. Simplely follow the input, using itertools could generate all combinations.

    import itertools
    digitals = [8, 4, 2, 1, 32, 16, 8, 4, 2, 1]
    def get_xtime(ps):
        h = 0
        m = 0
        for ...
    read more
  2. LeetCode: 195. Tenth Line

    The problem is here: https://leetcode.com/problems/tenth-line/. It's a bash programming, reading the 10th line of a file and output in stdout. I think there are lots of methods to do this, here I write some.

    If the file has more than 10 lines, I can output ...

    read more
  3. LeetCode: 112. Path Sum

    The problem is here: https://leetcode.com/problems/path-sum/ . Fancy that the number can be negative numbers, I made a stupid 'optimization'. I added a piece of code to check whether current node value is bigger than sum, assuming that every node value is positive. After droping this 'optimization', it ...

    read more
  4. LeetCode: 201. Bitwise AND of Numbers Range

    The problem is here: https://leetcode.com/problems/bitwise-and-of-numbers-range/ . I simply wrote a silly version and failed as expected, it just did exactly as this problem described.

    class Solution(object):
        def rangeBitwiseAnd(self, m, n):
            Attention! This is a bad version!
            :type m: int
            :type n: int
            :rtype: int
            return ...
    read more
  5. LeetCode: 235. Lowest Common Ancestor of a Binary Search Tree

    The problem is here: https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ . Using recursive function solve this fast, because of the nature of BST. For each node in BST, every node on left is smaller than root and every node on right. Therefore, the ancestor of two nodes in BST must be a ...

    read more
  6. LeetCode: 48. Rotate Image

    The problem is Rotate Image. If I can use more space, just copy the number for original matrix to new matrix. And the rule of rotate 90 degrees is simple, using original y coordinate as new x coordinate, lenght minus x as new y. After copying to new matrix, copy ...

    read more
  7. LeetCode: 125. Valid Palindrome

    The problem is 125. Valid Palindrome. It's quite simple to make a simple one.

    class Solution(object):
        def isPalindrome(self, s):
            :type s: str
            :rtype: bool
            if s == '': return True
            cs = 'abcdefghijklmnopqrstuvwxyz0123456789'
            is_char = lambda c: c in cs
            to_sstr = lambda s: ''.join([ c if is_char(c) else '' for c ...
    read more
  8. LeetCode: 412. Fizz Buzz

    The problem is Fizz Buzz. It's quite simple!

    class Solution(object):
        def fizzBuzz(self, n):
            :type n: int
            :rtype: List[str]
            ret = []
            for i in range(1, n+1):
                if i % 3 == 0:
                    if i % 5 == 0:
                elif i % 5 == 0:
                    ret ...
    read more