Python-剑指offer(10,11,12)矩形覆盖,二进制中1的个数,数值的整数次方

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35164554/article/details/82558468

题目:我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

环境:Python2.7.3

思路:又是一道斐波那契问题,自行理解即可

# -*- coding:utf-8 -*-
class Solution:
    def rectCover(self, number):
        # write code here
        num = []
        num.append(0)
        num.append(1)
        num.append(2)
        for i in range(3,number+1):
            num.append(num[i-1]+num[i-2])
        return num[number]
            

题目:输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

# -*- coding:utf-8 -*-
class Solution:
    def NumberOf1(self, n):
        # write code here
        return bin(n & 0xffffffff).count('1')

题目:给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

# -*- coding:utf-8 -*-
class Solution:
    def Power(self, base, exponent):
        # write code here
        mul = 1
        if exponent == 0:
            return 1
        elif exponent > 0:
            for i in range(exponent):
                mul = mul*base
            return mul
        else:
            for i in range(-exponent):
                mul = mul*base
            return 1/mul

猜你喜欢

转载自blog.csdn.net/qq_35164554/article/details/82558468
今日推荐