Other articles


  1. 卖火柴的小女孩算法题

    碰见这题的时候是一年前了。忙碌的一年下来再次回头撸一下,发现emmmm。。。竟然可以用dp做出来(bushi),有点欣慰额~~~

    # -*- coding: utf-8 -*-
    import copy
    
    class Solution(object):
        def makesquare(self, nums):
            """
            :type nums: List[int]
            :rtype: bool
            """
    
            if sum(nums) % 4: return False
            target = sum(nums) / 4
            if all([True if num <= target else False for num in nums]) == False:  # 检查是否有火柴大于边长
                return False
    
            # 不能用dp ...
    read more
  2. 200. Number of Islands

    class Solution(object):
        def numIslands(self, grid):
            """
            :type grid: List[List[str]]
            :rtype: int
            """
            newgrid = [list(x) for x in grid]
            ans = 0
            if not len(newgrid):
                return ans
            m = len(newgrid)
            n = len(newgrid[0])
            for i in range(m):
                for j in range(n):
                    if newgrid[i][j ...
    read more
  3. 199. Binary Tree Right Side View

    # Definition for a binary tree node.
    # class TreeNode(object):
    #     def __init__(self, x):
    #         self.val = x
    #         self.left = None
    #         self.right = None
    
    class Solution(object):
        def rightSideView(self, root):
            """
            :type root: TreeNode
            :rtype: List[int]
            """
            if not root:
                return []
            ans = []
            queue = [root]
            while queue:
                for i in range(len(queue ...
    read more
  4. 198. House Robber

    class Solution(object):
        def rob(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            #dp[i] = max(dp[i-1], dp[i-2] + nums[i])
            if not nums:
                return 0
            if len(nums) <= 2:
                return max(nums)
            dp = [0] * len(nums)
            dp[0] = nums[0]
            dp[1] = max(nums[0], nums[1])
            i ...
    read more
  5. 190. Reverse Bits

    # -*- coding:utf8 -*-
    class Solution:
        # @param n, an integer
        # @return an integer
        def reverseBits(self, n):
            new = 0
            for i in range(32):
                new = (new << 1) | ((n >> i) % 2)
                # n右移并%2得到二进制表达下的每位值。然后,new左移得到xxx0,'|' n的每位值,即xxx0可能变成xxx1。
            return new
    
    if __name__ == "__main__":
        print Solution().reverseBits(43261596)
    
    read more
  6. 189. Rotate Array

    class Solution(object):
        def rotate(self, nums, k):
            """
            :type nums: List[int]
            :type k: int
            :rtype: void Do not return anything, modify nums in-place instead.
            """
            # san bu fanzhuan fa
            k %= len(nums)
            if k == 0:
                return
            start1= 0
            end1 = len(nums)-k-1
            for i in range((end1-start1)/2 ...
    read more
  7. 187. Repeated DNA Sequences

    class Solution(object):
        def findRepeatedDnaSequences(self, s):
            """
            :type s: str
            :rtype: List[str]
            """
            n = len(s)
            d = {}
            res = []
            for i in range(n):
                substr = s[i:i + 10]
                d[substr] = d.get(substr, 0) + 1
            for key, val in d.items():
                if val > 1:
                    res.append(key)
            return res
    if ...
    read more
  8. 186. Reverse Words in a String II

    # Time: O(n)
    # Space:O(1)
    #
    # Given an input string, reverse the string word by word.
    # A word is defined as a sequence of non-space characters.
    #
    # The input string does not contain leading or trailing spaces
    # and the words are always separated by a single space.
    #
    # For example,
    # Given s ...
    read more

Page 1 / 21 »

blogroll

social