1. 174. Dungeon Game

    class Solution(object):
        def calculateMinimumHP(self, dungeon):
            """
            :type dungeon: List[List[int]]
            :rtype: int
            """
            # m = len(dungeon)
            # n = len(dungeon[0])
            # dungeon[m-1][n-1] = 1 - dungeon[m-1][n-1]
            # for i in range(m-1, -1, -1):
            #     for j in range(n-1, -1, -1):
            #         if i == m-1 and j == n-1:
            #             continue
            #         if ...
    read more
  2. 173. Binary Search Tree Iterator

    # Definition for a  binary tree node
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class BSTIterator(object):
        def __init__(self, root):
            """
            :type root: TreeNode
            """
            self.stack = []
            self.pushleft(root)
    
        def hasNext(self):
            """
            :rtype: bool
            """
            return self.stack
    
        def next(self):
            """
            :rtype: int ...
    read more
  3. 171. Excel Sheet Column Number

    class Solution(object):
        def titleToNumber(self, s):
            """
            :type s: str
            :rtype: int
            """
            alpha = [chr(x) for x in range(ord('A'), ord('Z')+1)]
            array = list(s)
            res = 0
            for a in array:
                res = res * 26 + alpha.index(a) + 1
            return res
    
    read more
  4. 170. Two Sum III - Data structure design

    # Time:  O(n)
    # Space: O(n)
    
    # Design and implement a TwoSum class. It should support the following operations: add and find.
    #
    # add - Add the number to an internal data structure.
    # find - Find if there exists any pair of numbers which sum is equal to the value.
    #
    # For example,
    # add(1 ...
    read more
  5. 169. Majority Element

    class Solution(object):
        def majorityElement(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            candidate = None
            count = 0
            for num in nums:
                if count == 0:
                    candidate = num
                    count += 1
                elif candidate == num:
                    count += 1
                else:
                    count -= 1
            return candidate
    if __name__ == "__main__":
        print Solution().majorityElement([1,2,2,3,3,3 ...
    read more
  6. 168. Excel Sheet Column Title

    class Solution(object):
        def convertToTitle(self, n):
            """
            :type n: int
            :rtype: str
            """
            ans = ''
            while n > 0:
                ans = chr(ord('A') + (n - 1) % 26) + ans
                n = (n - 1) / 26
            return ans
    if __name__ == "__main__":
        print Solution().convertToTitle(52)
    
    read more
  7. 167. Two Sum II - Input array is sorted

    class Solution(object):
        def twoSum(self, numbers, target):
            """
            :type numbers: List[int]
            :type target: int
            :rtype: List[int]
            """
            m = {}
            for i in range(len(numbers)):
                if numbers[i] in m:
                    return [m[numbers[i]]+1, i+1]
                else:
                    m[target - numbers[i]] = i
    
    read more
  8. 166. Fraction to Recurring Decimal

    class Solution(object):
        def fractionToDecimal(self, numerator, denominator):
            """
            :type numerator: int
            :type denominator: int
            :rtype: str
            """
            flag = 1
            if numerator * denominator < 0:
                flag = -1
                numerator = abs(numerator)
                denominator = abs(denominator)
            before = numerator/denominator
            remain = numerator - before * denominator
            if remain == 0:
                if flag == 1:
                    return str(before)
                else:
                    return '-' + str(before ...
    read more
  9. 165. Compare Version Numbers

    class Solution(object):
        def compareVersion(self, version1, version2):
            """
            :type version1: str
            :type version2: str
            :rtype: int
            """
            v1Arr = version1.split(".")
            v2Arr = version2.split(".")
            len1 = len(v1Arr)
            len2 = len(v2Arr)
            lenMax = max(len1, len2)
            for x in range(lenMax):
                v1Token = 0
                if x < len1:
                    v1Token = int(v1Arr[x])
                v2Token = 0
                if x ...
    read more

« Page 2 / 21 »

blogroll

social