Day28

  1. Distribute Candies
    思路:糖果数量是偶数,均分给弟弟和妹妹,要使妹妹分的糖果种类数最多。那就是当糖果种类超过总数量一半时,妹妹最多能获得1/2糖果总数的种类。反之,只能获得糖果的实际种类值。
class Solution(object):
    def distributeCandies(self, candies):
        """
        :type candies: List[int]
        :rtype: int
        """
        length = len(candies)
        dict ={}
        for i in range(length):
            # dict[candies[i]] = 1
            dict.setdefault(candies[i])
        if len(dict) > length/2:
            return length/2
        else:
            return len(dict)

【简洁版】

def distributeCandies(self, candies):
    return min(len(candies) / 2, len(set(candies)))

  1. Fizz Buzz
    思路:如果遇到3或者5的倍数,将字符换成别的字符串。
class Solution(object):
    def fizzBuzz(self, n):
        """
        :type n: int
        :rtype: List[str]
        """
        a = []
        for i in range(1,n+1):
            if i %3 == 0 and i % 5 == 0:
                a.append( "FizzBuzz")
            elif i %3 == 0:
                a.append("Fizz")
            elif i %5 == 0:
                a.append("Buzz")
            else:
                a.append(str(i))
        return a 

【简洁版】

def fizzBuzz(self, n):
    return ['Fizz' * (not i % 3) + 'Buzz' * (not i % 5) or str(i) for i in range(1, n+1)]

你可能感兴趣的:(Day28)